Js Code not working properly | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
21st Sep 2020, 10:02 PM
thatkidayo
thatkidayo - avatar
3 Answers
+ 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!
21st Sep 2020, 10:12 PM
Namit Jain
Namit Jain - avatar
+ 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
21st Sep 2020, 10:10 PM
Abhay
Abhay - avatar
+ 1
thank you Abhay and Namit Jain understood👍
21st Sep 2020, 10:14 PM
thatkidayo
thatkidayo - avatar