2

In the following for loop, how do I loop backwards and return all even numbers?

// How do I write a for loop that decrements the iterator while 
// outputting numbers in ascending order: 2, 4, 6, 8, 10


for (let i = 0; i < 11; i--) {
  if (i !==3,5,7,9){
    console.log(i); 
  }
}
Björn Nilsson
  • 3,703
  • 1
  • 24
  • 29
MiMiiXV
  • 49
  • 7
  • 1
    Possible duplicate of [How to determine if a number is odd in JavaScript](https://stackoverflow.com/questions/5016313/how-to-determine-if-a-number-is-odd-in-javascript) – isherwood Jan 22 '19 at 21:08
  • 5
    `for (let i = 11; i >= 0; i -= 2)` – Robert Harvey Jan 22 '19 at 21:09
  • 2
    Voting to reopen this since it's quite obvious what the OP wants (return even numbers between 0 and 11) using a decremental `for loop`. The question is not unclear at all. – AndrewL64 Jan 22 '19 at 21:27
  • You can either change the value that the function increments by ie decrement the loop counter by 2, instead of 1) or you can check if the current counter value is odd vs even. – SherylHohman Feb 17 '19 at 17:18
  • Is this what you are after? `var maxval = 11; for (var i = maxval - 2; i >= 0; i -= 2) { console.log(maxval - i); }` – Björn Nilsson Feb 17 '19 at 20:26

2 Answers2

1

You need to change the condition to reflect the expected output:

 

for (var i = 2; i < 11; i += 2) {
console.log(i);
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

The current for loop in the above snippet start at 0 and starts decrementing from the same, leading to negative value indexes.

Just start the loop's index at 11 and decrement from that index while checking if each index is an even number or not using a modulus operator like this index % 2 == 0.


Log all even numbers as a single string:

If the value is an even number, append it to the beginning of a string called say, even and then simply log each element in the even array to your console like this:

var even = ""; //assign an empty string for your even numbers

for (let i = 11; i > 0; i--) { // loop through all numbers between 0 and 11
  if (i % 2 == 0) { // check each loop value if it's an even number or not        
    even = i + ", " + even; // append the even numbers to the start of your "even" string
  }
}

// log each element in your "even" string to the console
console.log(even);

Log every ascending even number as a separate integer:

If the value is an even number, push it to the beginning of an array called say, even using the unshift() method and then simply log each element in the even array to your console like this:

var even = []; //assign an empty array for your even numbers


for (let i = 11; i > 0; i--) { // loop through all numbers between 0 and 11      
  if (i % 2 == 0) { // check each loop value if it's an even number or not
    even.unshift(i); // add the even numbers to the start of your "even" array using the unshift() method
  }
}

// log each value in your "even" array to the console
even.forEach(e => console.log(e)); 
AndrewL64
  • 15,794
  • 8
  • 47
  • 79