I don't really see how this could be wrong code.
In few examples, there is no output at all or it's only "10" in this one.
var num2 = 10;
while (num2 >= 10 && num2 <= 40 && num2%2===0){
console.log(num2);
num2++;
}
or like this:
var num2 = 10;
while (num2 >= 10 && num2 <= 40){
if (num2%2===0){
console.log(num2);
num2++;
}}
Your first loop stops after the first iteration because 11
is not an even number, so num2%2===0
is false
.
Your second loop never stops because it only increments num2
if it's even (from 10
to 11
), but 11
is not even and so num2
never changes.
Fix:
var num2 = 10; while (num2 >= 10 && num2 <= 40) { if (num2%2===0) { console.log(num2); } num2++; }
Ie always increment num2
, but only print the even numbers.
Alternatively:
var num2 = 10; while (num2 >= 10 && num2 <= 40) { console.log(num2); num2 += 2; }
Ie start at an even number and always increment by 2.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.