13

Below JavaScript loop for increment is working, but for decrement not. How to solve this?

for(var i=1; i<5; i++) { alert(i); } work fine

for(var i=10; i<5; i--) { alert(i); } not working
3
  • 1
    as an aside, you may find using console.log() to be an easier debugging tool than alert. If you're using Chrome, hit F12 to get dev tools, select console tab. Any console.log() output will go there (works similarly for IE, or FireFox w/ Firebug) Commented Apr 1, 2013 at 12:56
  • 1
    In the second statement, i is never less than 5. The for loop accepts 3 (optional) conditional statements. All of which must be truthy. Since i<5 is false on the first iteration, it causes execution to skip to first expression after the for loop. More Info Commented Oct 7, 2019 at 3:35
  • @gtzilla aside from his error, I'm having the same issue of it not working with i > 5... Commented Jun 5, 2022 at 21:02

6 Answers 6

34

Better check it with ease... for decrement use

for (var i = 10; i > 5; i--) { alert(i); }
Sign up to request clarification or add additional context in comments.

Comments

8

Your conditional check on your second for loop is incorrect. You are saying i=10, and continue while i<5, which would be never.

Try

for(var i=10; i>5; i--) { alert(i); } 

Comments

5

The first loop starts with i = 1 and increments, so that i = [1, 2, 3, 4] while i < 5. The second one starts with i=10, but the body is never executed, because it should only run when i < 5.

What you want is probably:

for (var i = 10; i > 5; i--) { alert(i); }

Comments

3

The second parameter of the 'for' loop is the exit condition. The code in the for loop is executed as long the condition is true. For example:

for(var i = 0; i < 10; i++){
  console.log([i]);
}
in the above for loop the exit condition checks if i is less than 10 (i < 10) which is true because in the first instance i = 0, as a result the loop goes into the code block and runs the code then increments and continues until the exit condition is no longer true.
You see why when you say: for (var i = 10; i < 5; i--){alert[i]} the code in the for loop is never executed because the exit condition i < 5 evaluates to false (i is equal to 10). To achieve your goal you've to change the exit condition for the for loop as:

for (var i = 10; i > 5; i--) {
    alert([i]);
}

Comments

1

You can make it more dynamic by setting i to the length of what you are iterating through:

const string = "abcd";

for (let i = string.length; i > 0; i--) {
   console.log(str[i]);
}

Notice: if you set the condition to i >= 0 you will be ignoring the last element in the array or string as it's 0 indexed base.

1 Comment

You wrote the opposite is i > 0 that will be ignoring 0 index
0

This is how reverse loops should be used (at least for this case):

const string = "abcd";

// first of all, `string[string.length]` will always be undefined
// you can't access what is not there, because the string is zero-base indexed

// so... we initialize a `len` const at `string.length - 1`

const len = string.length - 1;

// we loop in reverse
for(let i = len; i >= 0; i--){
    // but... it doesn't mean we can't read the characters
    // in an ascending order

    const char = string[len - i];

    // no more undefined
    // and this will print `a`, `b`, `c`, `d`
    console.log(char);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.