Why output is not possible here? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Why output is not possible here?

I was learning js course and when I see while loop, in this I see a ' try it yourself' Then I tab and think about this. https://code.sololearn.com/WkSvevkN1iNs/?ref=app

20th Mar 2019, 11:47 AM
Eknoor Singh
Eknoor Singh - avatar
5 Answers
+ 6
You're creating two infinite loops. var i=0; while (i<=10) var a=0; i is always <= 10, so this will keep executing the line "var a=0;". while (a<=20) { document.write(i + a + "<br />"); i++; } The value of a isn't changed within the loop, so the condition a<=20 will always be true and you've created yet another infinite loop. To prevent creating an inifinite loop, make sure that your loop has a valid break condition: var i = 0; while(i < 10) { i++; } This loop will repeat until i gets >= 10 and then it will stop.
20th Mar 2019, 12:11 PM
Anna
Anna - avatar
+ 5
That worked! Thank you all: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20th Mar 2019, 1:03 PM
boneSpider
boneSpider - avatar
+ 4
Eknoor Singh Gadria, Ulisses Cruz and Anna, I tried restructuring the loops like this but still couldn't form output in Code Playground. Does the JS have to be linked into the webpage in order to display output? var i=0; var a=0; while (i<10) { while (a<20) { document.write(i + a + "<br>"); a++; } i++; }
20th Mar 2019, 12:29 PM
boneSpider
boneSpider - avatar
+ 3
Eknoor Singh Gadria you have two infinite loops. The code is stuck in the first infinite loop. var i=0; while (i<=10) // <--- infinite loop (the loop will assign zero to 'a' while i is less or equal to 10, you should update 'i' to avoid this, for example) var a=0; while (a<=20) { // <--- infinite loop (you should update 'a' to get out of this loop, for example) document.write(i + a + "<br />"); i++; }
20th Mar 2019, 12:09 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 2
boneSpider try closing sololearn, reopen it and try again.
20th Mar 2019, 12:56 PM
Ulisses Cruz
Ulisses Cruz - avatar