While loop, trying to print reverse left triangle, unable to print line2, code is given below, done that with for loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

While loop, trying to print reverse left triangle, unable to print line2, code is given below, done that with for loop

var i = 7; var j = 0; while(i > 0){ while (j <i){ document.write("*"); j++; } document.write("<br/>"); i--; }

27th Feb 2018, 7:41 PM
Farhan Farooq
Farhan Farooq - avatar
1 Answer
0
Your program does not work as you expect it to because when it goes out of the nested while loop j = 7 and then i = 6; so the condition j < i is not met and your program will print various line breaks until the condition of the outter while loop is no longer true. With a for loop it actually works because all the three parts of the nested loop are executed, that is, initialization, condition and increment. If you want to do it with while loops you could try something like this: var i =7; var j =0; var s = 7; while (i > 0) { while (j<i) { document.write("*"); j++; } document.write("<br>"); s--; i--; j = i - s; }
20th May 2018, 1:53 PM
Jhon
Jhon - avatar