M
M
Muranx2020-04-27 10:47:54
JavaScript
Muranx, 2020-04-27 10:47:54

How to explain the operation of the indexOf( ) method in this case?

Hello ! Trite, but incomprehensible!

console.log('Widget with id and'.indexOf('Widget')); // 0 ok
console.log('Widget with id and'.indexOf('with')); // 7 ok 
console.log('Widget with id and'.indexOf('id')); // 1 wtf?
console.log('Widget with id and'.indexOf('and')); // 15 ok

console.log('Widget with id and'.indexOf('Widget')); // 0 ok
console.log('Widget with id and'.indexOf('with')); // 7 ok 
console.log('Widget with ids and'.indexOf('ids')); // 12 ok
console.log('Widget with id and'.indexOf('and')); // 15 ???

Why in the first block of code the occurrence id turned out to be 1??? because it is in the 12th position. . . And why in the second block of code idson the normal ( 12th position ) BUT for some reason anddid not move one position, but remained on the 15th, because we added one character ids ? I don't understand!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2020-04-27
@Muranx

Widget contains the id substring. What is not clear?
What about and. So you added the symbol in the wrong place.
I recommend to sleep))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question