this may seem stupid to some, but please try and help me see sense here, I'm currently learning javascript and one of my challenges is as follows
In countdown.js below, modify the while-loop with a conditional that will only allow a number to be printed if it is even. Your results should be the even numbers from 10 to 2 descending. Think carefully about how your code might decide if a number is even…
If I use the code below I get the answer 9, 7, 5, 3, 1, but if I change it to num + 1 in the log out it works. I thought the decrement would occur after the log out put?
var num = 10;
while (num > 0) {
if (num % 2) {
console.log(num); // can't be right??????
}
num--;
}
this may seem stupid to some, but please try and help me see sense here, I'm currently learning javascript and one of my challenges is as follows
In countdown.js below, modify the while-loop with a conditional that will only allow a number to be printed if it is even. Your results should be the even numbers from 10 to 2 descending. Think carefully about how your code might decide if a number is even…
If I use the code below I get the answer 9, 7, 5, 3, 1, but if I change it to num + 1 in the log out it works. I thought the decrement would occur after the log out put?
var num = 10;
while (num > 0) {
if (num % 2) {
console.log(num); // can't be right??????
}
num--;
}
Share
Improve this question
edited Aug 22, 2014 at 19:03
Paul Roub
36.5k27 gold badges86 silver badges95 bronze badges
asked Aug 22, 2014 at 19:01
caspe_ghostcaspe_ghost
851 silver badge5 bronze badges
1
-
5
if (num % 2 == 0) {
– Igor Commented Aug 22, 2014 at 19:03
5 Answers
Reset to default 7The test in your if
statement, num % 2
, is true when the value in "num" is not divisible by 2. The %
operator gives the remainder after a division, so when the remainder is non-zero, it's true
.
If you want even numbers, use !(num % 2)
, which will be true
when the remainder is zero, or perhaps more explicitly num % 2 === 0
.
0 == false, and 1 == true. % division returns the remainder, so 3 % 2 = 1, which evaluates to true. Negate your parision: if( ! num % 2 )
;
Alternately, use if( num % 2 === 0 )
, which may be more clear.
Use if (num % 2 === 0)
instead of if (num % 2)
.
All the other answers adequately explain the problem, but I want to go into the technical details.
Javascript converts between types, just, all the time1. The module operator returns an integer, but the if construct expects a boolean, so Javascript is going to convert between them.
This page shows how Javascript converts different types to booleans. As you can see, an integer value of 0 (which, in your case, will be when the number is even), will be converted to a boolean false.
The proper way to correct this, as others have noted, is to use the explicit test (num%2) === 0
(The ===
operator is strict equality, which is generally preferred by Javascript programmers for reasons that belong in another question)
1 EDIT: I used to use the word "constantly" here, but I replaced it because I didn't want to confuse my meaning with the puter science meaning of a constant-time operation
Well, for starters your num % 2 isn't doing anything. You can mod any number with any other number and it'll return something. What you're looking for is whether the number contained within num is divisible by 2. So, you'd need to do something like the following:
var num = 10;
while(num > 1) {
if (num % 2 === 0){
console.log(num);
}
num--;
}
I changed your while
statement to stop at 1 so that you only count down to the number 2, per your instructions.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745296343a4621165.html
评论列表(0条)