+ 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--; }
1 Odpowiedź
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;
}