Given an array Imp = ["g","o","s","u"] , using the method some() I need to get true , in response to a query about whether the source Imp array has an element "u"

By trial and error I got the desired result:

 Znp.some( function(value) { return value.indexOf("u") !==- 1 }) 

Now please explain in accessible language:


  1. Why Imp.some(function(value){return value.indexOf("u")==true}) does not work, Imp.some(function(value){return value.indexOf("u")==true}) it looks logical, we check if what is written in indexOf () is true
  2. Hmm, where did this figure 1 and -1 come from, is it some analogs of true and false in the body of callback functions?
  3. If I correctly understand the code value.indexOf("u")!==-1 , then for me it means something like the following: every value of the elements of the Imp array is checked, for compliance with the value "u" , and if ( !==-1 ) this value in the HE element is equal (I don’t know how to call this -1 , or an empty reference, or false), which means we display true , because at least one array element meets the requirements of the callback function
  4. Why the code value.indexOf("u")===1 does not work, because if I understand correctly, it sounds like, "I check the element of the array, is it equal to what is written as an argument in the indexOv?"

A request to the respondents to describe in more or less detail how it works, because if I don’t understand this elementary thing, there’s no point in loading me up with a lot of cleverness.

  • Imp = ["aag","bbo","ccs","ddu"] Is there a "u" element in this array? - PetSerAl
  • You ask to explain how your code works. Doesn't that seem strange to you? ;) - Nick Volynkin
  • Nick Volynkin, I do not understand anything, in my version of the array (which is displayed on me) everything is fine, some sort of nonsense, the array Imp = ["g", "o", "s", "u"] - Muranx
  • Nick Volynkin, your question seems strange to me, because I described the problem in great detail. Read carefully, I even broke everything point by point - Muranx
  • 2
    @Muranx, I advise you to read the documentation about how indexOf () works - Sergey Glazirin

1 answer 1

indexOf returns the index of the element in the array, and if there is no such element, it returns -1

  • thanks, already figured out! - Muranx
  • Does the author of the question call indexOf on an array? - PetSerAl