If / else if / else


#1


https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/0/2?curriculum_id=506324b3a7dffd00020bf661#


Keeps saying: Oops, try again. Your function returns true for 3! Did I do it all wrong or what is wrong? :o


var isEven = function(number) {
  if(number %2 === 0) {
    return "The number can be dived by 2!"; 
  }
  
  else if (isNaN(number)) {
      return "Please enter a valid number";
  }
  
  else {
    return "The number can't be divided by 2!";   
  }
};

isEven(2);


#2

you should return boolean values (true or false) for odd or even numbers, not strings


#3

Oh... Thanks a lot! My bad :blush:


#4

and strings are considered true, which explains the error message:
Your function returns true (string is true) for 3!


#6

I copied this code, verbatem (even using a code comparer) with only the text changed to boolean values. Still getting this error.

Here is my code

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

isEven(2);

#7

your else clause also returns true, while the else clause will run if the numbers are odd, so it should return false, and if the user doesn't provide a number, a string should returned (else if clause)


#9

var number=prompt("Enter your number");
var isEven = function(number) {
if (isNaN(number)) { return "Enter a corect number!" ;
}
else if (number % 2 === 0 ) {
return "true" ;
}
else {
return "false";}
};
isEven(number);

This is my code, but return there is an error which return true for 3! I don't know how to fix this. Can someone help me, pls...??


#10

var isEven = function(number) {
if (number % 2 === 0) {
return true;
}
else if (isNaN(number)) {
return "Input is not a number"
}
else {
return false;
}

};

isEven(test)


#11

actually, the last one "isEven(number)" , should put "" inside the paranthesis. that way, it'll show "Input is not a number"

Hope that helps


#12

Thank you so much, you saved me, finaly it works. :slight_smile: thx a lot :wink:


#13

Correct Code:
var isEven = function(number) {
// Your code goes here!
if(number % 2 == 0){
return true;
}else if(isNaN(number)){
return "String";
}else {
return false;
}
};
isEven(3)

it works as butter on bread.


#14

http://www.head-soccer.com/
I had quotes for true and false!

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

else {
return false;
}
};

isEven(5);

this worked.


#15

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.