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:
- 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 - Hmm, where did this figure
1and-1come from, is it some analogs oftrueandfalsein the body of callback functions? - 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 theImparray 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 displaytrue, because at least one array element meets the requirements of the callback function - Why the code
value.indexOf("u")===1does 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