is (i % 3 == 0) means ( i % 3 == false )?
Andrew Henderson
I am a beginner learning JS. Can anyone explain to me why "1" on the output?
here it is:
for (var i = 1; i <= 15; i++) { if (i % 2 == 0) { i += 2; } else if (i % 3 == 0) { i++; } console.log(i);
}output : 1, 4, 5, 8, 10, 11, 14, 16
I can figure out why the output equal to 4, 5, 8, 10, 11, 14, 16 , however, I don't understand why 1 is there as output...
42 Answers
When the value of i is 1, both conditional statement you defined doesn't get executed.
for (var i = 1; i <= 15; i++) { if (i % 2 == 0) { console.log( 'inside if' ); i += 2; } else if (i % 3 == 0) { console.log( 'inside else-if' ); i++; } else { console.log( 'neither if nor else-if' ); } console.log(i);
}Remainder is always 1 when you divide it with 2 or 3, which is not equals to 0.
console.log( 1 % 2 );
console.log( 1 % 3 ); 3 When you look at your code the console is out of if-else condition, means that it is printing i from condition and out of condition, Your for-loop start from 1, when i=1, condition skipped and console executed and print i which is equal to one I think that's why And I % 3 == 0 means I % 3 == false because
var i = 2;
if (i % 2 == 0)
{ console.log(0)
}
if (i % 2 == false)
{ console.log(0)
}all the answer is 0
1