If you put 2 in the answer | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

If you put 2 in the answer

the output will be 02468101214161820 if you use the number 2 in the code: var x = 0; for (; x <=20; x +=2) { document.write(x); } someone help me to understand it

5th Nov 2016, 9:17 PM
Alexandre Kruschewsky
Alexandre Kruschewsky - avatar
7 Réponses
+ 3
Each value of x is written on the screen as you put the code "document.write (x);" within the FOR Loop. To fix this, just get it out of the for loop: var x = 0; for (; x <= 20; x + = 2) { } document.write (x); Output: 22 Hope it helps.
5th Nov 2016, 10:41 PM
Paulo Castro
Paulo Castro - avatar
+ 2
Why we must puts 2, not 4 or 5? Me Ask for sololearn example... x += _
17th Dec 2016, 10:43 PM
Vladimir Pavlychev
Vladimir Pavlychev - avatar
+ 2
the reason for 2 is because the question asks that "even" numbers are printed only. So 0, 2, 4...etc. Your starting variable (initialization) is 0, this then is to be incremented or increased by 2 so long as the condition (is less than or equal to 20) is true which is why your variable increments as follows: x+=2 (same as x = x + 2) So for example you start at x = 0 (0 is printed), next x = 0 + 2, x now becomes 2
24th Aug 2018, 11:43 AM
Shadi N Alkasem Almjdlani
Shadi N Alkasem Almjdlani - avatar
+ 1
x = 0 while x <=20 : print (x) x += 2
28th Mar 2020, 9:36 PM
Thandinkosi Mbatha
Thandinkosi Mbatha - avatar
0
because you write document.write(x); } INSIDE your loop
4th Dec 2016, 5:15 AM
Mohammed Shalan
Mohammed Shalan - avatar
0
hello guys. I am sorry but I don't understand why not 3 or 4 or 5... 0 3 6 9 12 15 18 .... ! why would not it be good ? Thanks.
1st Apr 2019, 3:51 PM
Thibaut Fleuriot
Thibaut Fleuriot - avatar
0
same question
25th Nov 2019, 9:52 PM
Hamad Al-saleem
Hamad Al-saleem - avatar