+ 1
Js Code not working properly
3 ответов
+ 5
thatkidayo
See this!
https://code.sololearn.com/WMuLcC0t60sM/?ref=app
Take care of such cases!
Always use i--
Because, we don't want variables to get skipped, as the list becomes smaller but i doesn't change according to that!
Earlier,
l = [1,3,"a","b",2] and i = 0
Then,
l = [1,3,"a","b",2] and i = 1
Then,
i = 2
So, l = [1,2,"b",2]
Then,
i = 3
And i didn't check for "b" it directly went to 2 as l[3] is 2 and we missed that "b"
So we want that i to decrease by 1 when we remove an element from the list!
+ 3
It is actually working as it should if you observer carefully, splice changes the original array ,so when i value is 2 ,a is removed from the array and you are now left with 1,3,b,2
Next when i value is 3 the next item in array is 2
+ 1
thank you Abhay and Namit Jain
understood👍