Home > OS >  why does this for-loop only output once?
why does this for-loop only output once?

Time:12-23

  • SOLVED in the comments, thanks guys! *

My first post. Thanks in advance.

why does my for loop only out 3 when i console.log(i)? Isn't it supposed to output: 1,3,5,7,9?

for (let i = 0; i < 10; i  ) {
  if (i % 2 == 0) continue; 
}
console.log(i) 

Best Jesper

CodePudding user response:

Your current console.log is outside the scope of your for loop. You most likely have i defined elsewhere and set to 3. if it were in the for loop, it would post your expected results.

//example 1
var i = 3

for (let i = 0; i < 10; i  ) {
  if (i % 2 == 0) continue; 
}
console.log(i) 


//example 2


for (let i = 0; i < 10; i  ) {
  if (i % 2 == 0) continue; 
  console.log(i) 
}

CodePudding user response:

your console.log is placed at the end of loop outside this one so only last value is displayed

var i = 0;
for (i = 0; i < 10; i  ) {
  if (i % 2 == 0) continue;
  console.log(i);
}

  • Related