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));