I find this simple "for loop" exercise.
Using a for loop print all even numbers up to and including n. Don’t include 0.
let n1 = 22; // Example output: // 2 4 6 8 10 12 14 16 18 20 22 OR each item on a new line
I found a way to solve this but I don't think its elegant at all.
Here is my code:
let n1 = 22;
for (let i = 0; i < n1; i ){
let b = i * 2;
if (b <= n1){
if (b == 0) {} else
{ console.log('Count',b);}
};
};
How can I improve it?
CodePudding user response:
Just count by twos:
for (let i=2; i <= 22; i =2) {
console.log('Count', i);
}
CodePudding user response:
for (let i=2; i <= 22; i =2) {
console.log(i);
}
all the best
CodePudding user response:
Here using while loop:
const count = { index: 0, n1: 22 }
while(count.index < count.n1) {
console.log('Count', count.index = 2)
}