Venu Madhav Venu Madhav - 4 months ago 10
Javascript Question

I need a javascript for loop it should print 10 to 1 like it should print still 1

I need a javascript for loop it should print 10 to 1 like it should still 1.

My JS Code:

var b = "";
var inc = "";
var k = 10;
for(a = k;a >= 1; a--){
for(i=k; i<=1; a++)
{
console.log(parseInt(inc));
}
}
console.log(parseInt(b));


What am expecting is:


10 9 8 7 6 5 4 3 2 1

9 8 7 6 5 4 3 2 1

8 7 6 5 4 3 2 1

7 6 5 4 3 2 1

6 5 4 3 2 1

5 4 3 2 1

4 3 2 1

3 2 1

2 1

1

Answer

Use following loop,

for (i = 11; i > 1; i--) {
    for (j = i - 1; j > 0; j--) {
        console.log(j)
    }
}
Comments