indexOf to some() javaScript

indexof javascript
indexof javascript array
javascript find index of object in array
javascript indexof array of objects
includes javascript
filter javascript
javascript array find index
javascript some vs find

I'm trying to convert the function below to use the .some() method but I don't understand how this works. I've aready read the MDN documentation. Can anyone help me?

function guessRepeatValidate(userGuess) {
    let passed = true;
    if (guessArchive.indexOf(userGuess) > -1) {
        passed = false;
    return passed;


Here is how to convert.

let guessArchive = ['test', 'some'];

function guessRepeatValidate(userGuess) {
    return !guessArchive.some(i => i===userGuess);



Array.prototype.indexOf(), Here is how to convert. let guessArchive = ['test', 'some']; function guessRepeatValidate(userGuess) { return !guessArchive.some(i� The indexOf () method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found. JavaScript Demo: String.indexOf ()

const guessRepeatValidate = (userGuess, guessArchive) => !guessArchive.some(i => i === userGuess);

const guessArchive = ['one', 'two', 'three'];

console.log(guessRepeatValidate('one', guessArchive));

console.log(guessRepeatValidate('four', guessArchive));

indexOf to some() javaScript, More "Try it Yourself" examples below. Definition and Usage. The indexOf() method searches the array for the specified item, and returns its position. The search� The some() method checks if any of the elements in an array pass a test (provided as a function). The some() method executes the function once for each element present in the array: If it finds an array element where the function returns a true value, some() returns true (and does not check the remaining values)

The accepted practice for this kind of problem would be using includes, however, if you really want to use some:

function guessRepeatValidate(userGuess) {
    return !guessArchive.some(e => e == userGuess);

JavaScript Array indexOf() Method, JavaScript String indexOf() Method The indexOf() method returns the position of the first occurrence of a specified value in a string. This method returns -1 if the value to search for never occurs. Note: The indexOf() method is case sensitive. The indexOf() always perform a case-sensitive search. To find the index of the last occurrence of a substring in a string, you use the lastIndexOf() method. JavaScript String indexOf() examples. Let’s take some examples of using the indexOf() method. 1) Using indexOf() method example

JavaScript String indexOf() Method, The findIndex() method returns the index of the first element in an array that pass a test (provided as a function). The findIndex() method executes the function once � The JavaScript indexOf() method is the best way to find out if one string exists inside of another. If it does, the starting position of that searched string is returned. If the sub-string does not exist, then “-1” is returned; this is a reliable indicator of “does not exist”. Helpful links for the JavaScript indexOf() method

JavaScript Array findIndex() Method, If it finds an array element where the function returns a true value, some() returns true (and does not array.some(function(currentValue, index, arr), thisValue)� IndexOf allows us to search a string in JavaScript for a match. So what is this number being returned to us? It’s the index of the first character’s position when there is a match. So how do we use it? Typically we would use indexOf combined with a conditional statement - but before we go there, consider this example:

JavaScript Array some() Method, <script>. // Taking input as an array A. // having some elements. var A = [ 1, 2, 3, 4, 5 ];. // indexOf() method is called to. // test whether the searching element. The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back.

  • .some is not appropriate here. guessRepeatValidate = guess => !guessArchive.includes(guess)
  • Thanks for your reply. I know, but .som() is a requirement. It MUST replace the indexOf.
  • Per @CertainPerformance some() performs an operation/evaluation that you pass in. Where in includes() is taking a value (which is what you need).
  • It's possible to use .some here, it's just really weird
  • @CertainPerformance Homework. It's almost always homework :).
  • Great @Bibberty, this works beautifully. And I would never use an arrow function to it. It's a great example on how to apply this approach.