0

someone briefly explain to me this code because i don't know why am getting 7

<!DOCTYPE html> <html> <body> <h2>JavaScript String Methods</h2> <p>The indexOf() method returns the position of the first occurrence of a specified text:</p> <p id="demo"></p> <script> var str = "Please locate where 'locate' occurs!"; var pos = str.indexOf("locate"); document.getElementById("demo").innerHTML = pos; </script> </body> </html>

19th Feb 2019, 10:13 PM
Batte Fred
Batte Fred - avatar
4 odpowiedzi
+ 2
Thinking of the string as an array (with "P" having an index of 0, "l" having an index of 1, etc.), counting up to the first letter in the first occurrence of the word "locate" results in the function returning 7 (the "l" in locate is located at the 7th index within the string).
19th Feb 2019, 11:04 PM
Faisal
Faisal - avatar
+ 2
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”.
7th Mar 2019, 3:50 PM
A͢J
A͢J - avatar
0
If you say, "var pos" meaning you want to identify the number of that term Since for an array, we begin counting from '0' and therefore the pos of the term "locate " l is at 7 That's why the answer is 7.
20th Feb 2019, 3:31 AM
Norbert Oketch
Norbert Oketch - avatar
0
means you start counting from index?
20th Feb 2019, 1:26 PM
Batte Fred
Batte Fred - avatar