Can tell me hiw does it work?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can tell me hiw does it work??

function del(){ var num = document.getElementById("input").value, str = ""; var arr = num.split(""); for(var i=0; i<arr.length-1; i++) { str += arr[i]; } document.getElementById("input").value = str; } and plz give an example with " for"

12th Mar 2018, 10:22 PM
Sina
Sina - avatar
12 Answers
12th Mar 2018, 10:40 PM
LordHill
LordHill - avatar
+ 1
i is a variable. each run of the for loop, i is whatever number the loop is currently on. so the first time the loop runs, i=0 .. the second time the loop runs, i=1, and so on until i meets the condition that tells it to stop. in this case, it runs as long as i<arr.length
12th Mar 2018, 10:35 PM
LordHill
LordHill - avatar
+ 1
check the code again, I added more to it
12th Mar 2018, 10:45 PM
LordHill
LordHill - avatar
+ 1
Thanks
12th Mar 2018, 10:47 PM
Sina
Sina - avatar
+ 1
you loop and str= arr[i] makes str whatever the new loop says. what you want instead is str= str+arr[i] This way it adds the new piece to str instead of making str the new thing
12th Mar 2018, 10:58 PM
LordHill
LordHill - avatar
0
When it add's a one to i so what does i do??
12th Mar 2018, 10:33 PM
Sina
Sina - avatar
0
Can you make it with web plz
12th Mar 2018, 10:37 PM
Sina
Sina - avatar
0
Thanks
12th Mar 2018, 10:40 PM
Sina
Sina - avatar
0
Ok underestood
12th Mar 2018, 10:41 PM
Sina
Sina - avatar
0
Thank you so much
12th Mar 2018, 10:42 PM
Sina
Sina - avatar
0
Ok
12th Mar 2018, 10:47 PM
Sina
Sina - avatar
0
I tried my code it delet's s.th just I don't underestand in that code Wrote's "i++" but it delet's one character
12th Mar 2018, 10:49 PM
Sina
Sina - avatar