+ 1

Why Zero?

var thor = "Thor: Ragnarok is fucking awesome!"; var summary = thor.lastIndexOf("Thor"); console.log(summary); - The answer should be 6. But on the Code Playground the answer is zero. Can someone explain me this?

27th Jan 2018, 12:43 PM
Vincent Matthew Benito
Vincent Matthew Benito - avatar
2 Answers
+ 7
lastIndexOf returns the index at the beginning of the string that was searched for. So in this case 'Thor' starts at index 0. If you searched for 'Ragnarok' it would have returned 6
27th Jan 2018, 12:52 PM
Stewart
Stewart - avatar
+ 3
the method lastIndexOf searches for string like "Thor" in your variable. So you just have used the word Thor one time so it will give out you the first index of the last word which equals "Thor" another example: "Thor Thor".lastIndexOf("Thor"); //would be 5
27th Jan 2018, 12:54 PM
Leander
Leander - avatar