IndexOf (Java Script)?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
13 views
Hello everyone!
Please help me to understand.
Or have I misunderstood or it's a misprint. (Though probably misunderstood)

The documentation says that
str.indexOf(searchValue[, fromIndex])
searchValue - is, as I understand it, a piece of string, a start index where you need to find a string
and fromIndex is the index where we should start the countdown.

QUESTION:
'Blue whale'.indexOf('whale', 0); // returns 6
Need to find the index of the beginning of the word "kit" in a line "Blue whale", where the reference point is index 0 Turns 6.
S - 0, I - 1, n - 2, I - 3, j - 4, gap - 5, K - 6.
This is understandable.
---
Why here the answer is also 6, when the starting point is the index of 5?
'Blue whale'.indexOf('whale', 5); // returns 6

Where is the account? Something I do not understand )

Here's another:
'Blue whale'.indexOf(", 9); // returns 9
'Blue whale'.indexOf(", 10); // returns 9

Why 9?

Thank you in advance!
by | 13 views

2 Answers

0 like 0 dislike
All right turns. Because it returns the character position of the given string and the second argument does not cut the line, it just begins beeping in the desired character, it will not affect the return value (only for search). You get the number of character in your string.
UPD:
About

'Blue whale'.indexOf(", 9); // returns 9 'Blue whale'.indexOf(", 10); // returns 9

Here the situation is that the start room search greater than or equal to the length of the string (in which you are looking for). And specifications, if the search string has zero length, and the initial search index more or equal length of the original string, the function always returns the starting index of search. If you do so:
'Blue whale'.indexOf('C', 10);
The search string is not zero-length, and if the initial search is greater than or equal to the length of the string, the specification is always returned -1.

I hope correctly explained, if you need something.
by
0 like 0 dislike
'Blue whale'.indexOf('whale', 5); // returns 6
'Blue whale'.indexOf(", 9); // returns 9
'Blue whale'.indexOf(", 10); // returns 9
It is true in fact. It returns where the desired symbol.
More clearly:
'Sikit kit'.indexOf('whale', 5); // returns 6
I.e., the first 5 is not a search, but their indexes are taken into account.
by

Related questions

0 like 0 dislike
2 answers
asked Sep 3, 2019 by antimodern
0 like 0 dislike
2 answers
asked Aug 16, 2019 by antimodern
0 like 0 dislike
2 answers
asked Jul 13, 2019 by antimodern
0 like 0 dislike
2 answers
asked Jul 7, 2019 by antimodern
0 like 0 dislike
2 answers
110,608 questions
257,187 answers
0 comments
40,796 users