Łukasz Buławski
Łukasz Buławski

Reputation: 49

JS loop - how can i decrement from 1 to 100

Below is a loop that decrements. How can I rewrite this to count from 1 to 100:

for (var i =100; i > 0; i--) {

    console.log(from 1 to 100);

}

Upvotes: 3

Views: 5485

Answers (3)

ProfDFrancis
ProfDFrancis

Reputation: 9441

How to increment from 1 to 100

for(var i =1;i<=100;i++){
  console.log(i);
}

How to decrement from 100 to 1

for(var i =100;i>=1;i--){
  console.log(i);
}

Only the last 50 lines are shown in the console in Stack Overflow

Any previous output lines scroll off the top and are lost. So although the output may look incomplete, they were indeed all logged in the console.

Upvotes: 7

Nina Scholz
Nina Scholz

Reputation: 386756

You could take the delta of 101.

for (var i = 100; i > 0; i--) {
    document.getElementById('out').innerHTML += (101 - i) + '\n'
}
<pre id="out"></pre>

Upvotes: 3

Scath
Scath

Reputation: 3824

This will work but the snippet will only show 50-100.

for(var i=1;i<=100;i++){
console.log(i);
}

Upvotes: 1

Related Questions