Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why does this for-loop only output once?

  • 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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) 
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading