Newbie need help

hi
ive some problem to understand this code from tutorial hope u guys can help me out to understand it.

i = 0;
while (i <=10) {
    if (i %2)
    console.log(i)
    i++
}

my question is why do i get odd numbers from the console?
i know what the % means but i wanna know why it display a odd number in the console ?
in the if conidtion there are only 1 or 0
so 1 = true therefor console.log else 0 = false therefor i++ ?

Hi,
this code is wrong as is.

if (i %2) is not boolean isn’t it rather if (i %2 === 0) ?

Now what it does is very simple.

// Initialise an identifier i
i = 0;
// Loops if and as long as the condition on i is met
while (i <=10) {
    // Checks if the current value of i is even
    if (i %2 === 0)
        // Display the current value if i to the console.
        console.log(i)
    // Increments the value stored in i
    i++
}

Make sure to pay attention to scope. Namely make sure to notice the console.log(i); line will be executed only when i is even.

Overall your while scope will execute as long as the condition is met. Namely as long as i is not equal to 10 or bellow.

Starting from 0, i++ will add 1 to the value on each loop. Eventually the value will reach 10 to execute a last time. By the end of that last loop i will reach the value 11. On the next comparison at the while statement level, i being above 10, it will exit the loop.

Hope this helps. Should you have further questions, please ask.

Regards.

thanks for the reply.
yeah now i see why my thought process is wrong
the console.log need to be in scope of the if condition so its only giving me an output if its true.
the i++ its outside the scope of the if condition so its doing until the condition from the while loop is met <=10 .

I think you got it but just to be sure we are on same page.

Your scopes are correct as is. When there is no curly braces to delimit the scope on a few statements, such as if, the next statement only is considered being part of the code.

function myFunction(){
  if (true)
    console.log("I am part of if scope");
    console.log("Am I part of if scope?");
  callSomeMethod();
}

is equivalent to

function myFunction(){
  if (true) {
    console.log("I am part of if scope");
  }
  console.log("Am I part of if scope?"); // Now it's clearly no.
  callSomeMethod();
}

Indeed it is not Python which deals on indentation to determine scope. In JS and many popular languages, we use curly brackets and exceptionally a few statements allow for no brackets when they are made of only one statement.

I feel comfortable with that but I hear some consider not using brackets even on single line code is a bad practice. I’d rather say format your code properly and there should be no issue. The 1st snippet above was purposely badly written so that you can observe that point. Should be written more like this:

function myFunction(){
  if (true)
    console.log("I am part of if scope");

  console.log("Am I part of if scope?"); // 1 empty line above and indentation makes it clear it is not.
  callSomeMethod();
}

yeah thanks i got it know

i = 0;
while (i <=10) {
    if (i %2 === 1) {
        console.log(i)
    }
    i++
}

i should writing like this in the future its easy for me to follow.
thanks again much appreciate :smiley:

Not sure if you are there yet, but that while loop can be simplified as a for loop:

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

hi
yeah im already in that section of the tutorial.
for me it was not clear how the line of code that i wrote renders.
i couldn`t wrap my head around why it trigger the console.log and the i++
but after this example it was clear to me

function myFunction(){
  if (true) {
    console.log("I am part of if scope");
  }
  console.log("Am I part of if scope?"); // Now it's clearly no.
  callSomeMethod();
}
2 Likes

i % 2 is 0 for even is and 1 for odd is. Since 1 is truthy your code will log the odd values.