0

Help The for loop (JS)

Here is the quizz I don't understand : Fill in the blanks to print even values from 0 to 20 using a for loop: var x = 0; for (; x<= ??? ; x+= ???) { document.write(x); } The ??? are the two answers I need to find but I don't understand the third statement (x+=). I know x+=y means x=x+y but in this exercice it confused me. Thanks

9th May 2021, 1:43 PM
Philippe Graas
3 Answers
+ 8
Hello there! The 3rd statement is executed each time the loops is executed. For example, if it's x++, every time x will increase his value of 1. In this case, you need to find even values (2, 4, 6,....), so a hint: make a loop that will print all even numbers, by 'skipping' by 2 numbers every time. :) For more: https://www.sololearn.com/learn/JavaScript/1140/?ref=app
9th May 2021, 2:13 PM
Matthew
Matthew - avatar
+ 1
Thanks both of you guys, your explanations are clear and help me a lot. Now I get it but I was so frustrated that I was stuck in...a bad loop ;) Thanks again! :)
10th May 2021, 8:07 AM
Philippe Graas
0
Philippe Graas You have to get even numbers till 20 so there should be x <= 20 and even numbers are those numbers which are multiple of 2 so just do x += 2 finally for(; x <= 20; x += 2) x += 2 means x = x + 2 so every time 2 will be increment in previous x value
9th May 2021, 2:11 PM
A͢J
A͢J - avatar