1. If/else


#1

I get the error: "Oops, try again. Looks like your function returns false when number = 3. Check whether your code inside the if/else statement correctly returns true if the number it receives is odd."

I don't understand this error, isn't getting false when the number for 3 correct because 3 is not divisible by 2 (as whole numbers).

Here is my code:

var isEven = function(number) {
if (number % 2 === 0) {
return "true";
}
else {
return "false";
}
};

console.log (isEven ());
console.log (isEven (3));
console.log (isEven (4));

I get false, false, true respectively.

Thanks


#2

@tyoungg take a look at your code here I have commented where the error is

var isEven = function(number) {
if (number % 2 === 0) {
    // here you might wanna return the protected
    //namespace(boolean) true rather than the 
    //string "true"
return true;
}
else {
    // here you might wanna return the protected
    //namespace(boolean) false rather than the 
    //string "false"
    return false;
} 
};

console.log (isEven ());
console.log (isEven (3));
console.log (isEven (4));

#3

@rydan

Thanks so much! :smiley:


#4

"if ((number % 2) === 0)"

Can someone explain why we use "=== 0" in this? I get the modulo usage, but I do not get why we need "=== 0".


#5

Well what characterizes an even number? Well you can divide it by 2 without rest number%2 gives you the rest and without rest means that the rest is 0 and that is what you check for.


#7

I don't get why we use === 0 too. My code:

if (number % 2) {
And I gave a (number) atribution of 4. So 4 is divisable by 2 evenly. We get answer of 2 which is even. So it should return true without === 0. So what is the reason we use it?


#8

Yes 4 is evenly divisable by 2 therefore the remainder is 0. Meaning you're condition is 0 and you might know that 0 and 1 where the original values for on/off and true/false therefore 0 means false. So you need to adjust either your condition to !(number%2) or the output to false.


#9

Yeah, I know 0/1 meanings. Probably I'm not sure what % really means here?


#10

% means modulo which computes the remainder of a integer division e.g. 9/4 is 2.25 and if you only use full integer its 9/4 = 2 + 1 (/4) . So 4 fits 2 times in 9 and 1 still remains to be processed and that is the number that modulo computes 9%4 = 1, 5%3 = 2, 8%2 = 0 aso


#11

Oh, now I guess I understand. So, % means that it checks if 4 divided by 2 (in my case) gets any remainder and because it divides evenly we give a condition if it is false that it has remainder and because it is false it returns true. Thanks.


#12

we are asked not to use console.log
try isEven(); instead


#13

this was genius thank you


#14

I type this code:
var isEven = function(number) {
if (number % 2 === true){
return "True";
}
else {
return "False";
}
};
isEven(3)
and get this error message

Oops, try again. Looks like your function returns false when number = 3. Check whether your code inside the if/else statement correctly returns true if the number it receives is odd.


#15

I tried this other way
var isEven = function(number) {
if (( number % 2 != 0) === true){
return "True";
}
else {
return "False";
}
};
isEven(3);

and still receive the same error


#16

hi try that

var isEven = function(number) {
  // Your code goes here!
  if (number % 2 === 0) {return true ;}
  else {return false; }
};

#17

that´s the first I tried when I got the error, and thoght I had to insert the numbers and still had the same error....now it worked.
Thank you for your help


#18

read the instructions carefully again and you will know where they meant not to use console.log()