Im currently in process of learning Javascript. And I have seen the following code, which confuses me.
Description of code:
Starting on line 1, the function isOdd takes a number n and returns a boolean (true or false) stating whether the number is odd or not.
Code
var isOdd = function (n) {
if (n % 2 === 0) {
return false;
} else {
return true;
}
};
var isEven = function(n) {
if(n % 2 === 0) {
return true;
} else {
return false;
}
};
Where I am confused.
The code:
n % 2 === 0
I have always taken the following to be the description for %:
% Is the modulus operator. It returns the remainder of dividing number1 by number2.
Which would mean that the if statement in the function isOdd returns false is the difference between n and 2 is 0. But its meant to mean if n is divisible by 2 (even) return false.
I just dont see how its doing that.
In my mind, if we take the even number 30. Apply it to n % 2. We get 15, which is remainder of dividing 30 by 2. 15 does not equal 0, but 30 is an even number, and with this code it would be seen as odd.
Can someone explain this?
Im currently in process of learning Javascript. And I have seen the following code, which confuses me.
Description of code:
Starting on line 1, the function isOdd takes a number n and returns a boolean (true or false) stating whether the number is odd or not.
Code
var isOdd = function (n) {
if (n % 2 === 0) {
return false;
} else {
return true;
}
};
var isEven = function(n) {
if(n % 2 === 0) {
return true;
} else {
return false;
}
};
Where I am confused.
The code:
n % 2 === 0
I have always taken the following to be the description for %:
% Is the modulus operator. It returns the remainder of dividing number1 by number2.
Which would mean that the if statement in the function isOdd returns false is the difference between n and 2 is 0. But its meant to mean if n is divisible by 2 (even) return false.
I just dont see how its doing that.
In my mind, if we take the even number 30. Apply it to n % 2. We get 15, which is remainder of dividing 30 by 2. 15 does not equal 0, but 30 is an even number, and with this code it would be seen as odd.
Can someone explain this?
Share Improve this question edited Apr 18, 2012 at 10:19 RSM asked Apr 18, 2012 at 10:14 RSMRSM 15.2k36 gold badges99 silver badges145 bronze badges 4- "if statement in the function isOdd returns false is the difference between n and 2 is 0" It doesn't mean the difference is 0 (that would mean they're the same number). It means that the remainder of dividing n/2 is 0 ;) I hope that helps – DiogoNeves Commented Apr 18, 2012 at 10:19
- 30%2 is zero, i.e. whatever is left after 30/2 (30 divided by 2 is 15). – Tom Commented Apr 18, 2012 at 10:23
- 30%2 is not 15! 30%2 is zero. That's what "remainder" means - whatever "remains" after division. – Tom Commented Apr 18, 2012 at 10:25
- read here about modulo en.wikipedia/wiki/Modulo_operator – A.B.Cade Commented Apr 18, 2012 at 10:32
3 Answers
Reset to default 3The line in question:
if (n % 2 === 0) {
return false;
}
Means "if when you divide n by 2 the remainder is zero, then return false (i.e. n is not odd)".
The "remainder" is whatever is left over when you subtract the nearest multiple, so for example "64 % 10" is 4, since the nearest multiple of 10 is 60, leaving 4.
Taking your example and putting it another way, 30/2 is 15, 30%2 is zero (i.e. whatever is left over after 30/2). Here's more info on the remainder after division.
You're confusing Quotient and Remainder. When you divide 30 by 2, integer quotient is 15, and remainder is 0. You can also calculate remainder by multiplying integer quotient by divisor and subtracting it from dividend. So for this division remainder is 30 (dividend) - 15 (quotient) * 2 (divisor) = 0.
If n can be divided by 2 it means it's even ->
which means it's not odd ->
isOdd is false
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745401171a4626108.html
评论列表(0条)