More on Control Flow in JS / If / else if / else - HELP!


#1


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

Hey CC Squad,

Can you guys help me out with this please? Really can't figure out why this piece of code isn't working, I've tried to see what it is that's not working and am just stuck. Any help would be hugely appreciated. Thanks!


var isEven = function(number) {
  
  if ( number % 2 === 0 ) {
      return true;
      
      } else if (isNaN(number) === true); {
              return "This ain't a number dawg";
              } else {
                  return false;
                  }  
             
}


#2

So the question is asking you to return Boolean values (true or false) so when you use "This ain't a number dawg"; (string). It makes you fail the exercise.

You also at the bottom of your code need to call your function with a number inside it.
There are also a few unnecessary things you are doing in your code.

Study my code below and look at the differences.

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

Let me know if you need further explanation. :slight_smile:

If this helped please mark my reply as the solution.


#3

Hey @oscartbeamish thanks for replying. I think I was attempting to be a bit too fancy without reading properly the Q. The error that I'm getting at the moment is

'SyntaxError: Unexpected token else'. It doesn't like the final else condition but i'm not sure why. Any clue?

Cheers!


#4

Make sure the final else is inside the curly brackets of your function.


#5

If you are still getting an error please post your new code.


#6

@oscartbeamish hey man, so i basically left it alone, went to sleep woke up and changed it and it seems to work now. I took out the flnal else, checked my brackets and added the boolean with a string, then tested a few times. Thanks again for your help, its really appreciated!

So... :

var isEven = function(number) {
if (number % 2 === 0) {
return true + ' Muthafucccaaa';
} else if (isNaN(number)) {
return "This ain't a number dawg";
}
};

isEven('est');
isEven('zero');
isEven(1000000);


#7

var isEven = function(number) {
if(number % 2 === 0) {return true}
else if(isNaN(number) === 1) {return "Not a number at all"}
else {return false}

// Your code goes here!

};

Does not work and stuff. How do I "return" a string? That is what the website is asking you to do right??
Oops, try again. Make sure your if/else if/else statement returns a string when the function input isn't a number.


#8

Sorry to intervene here, but that is incorrect oscartbeamish. The question states: "the function should return a string that tells the user that their input isn't a number." So you should return a string and not merely return Boolean values true or false. Using the example of ilovethedrama. Please see below:

var isEven = function(number) {
if (isNaN(number))
return ("This ain't a number dawg");

else if(number%2 === 0)
return(true);

else{
return(false)}
};

isEven('dawg');


#9

Yeah you are so right I stupidly used my memory to answer this one rather than looking at the question.
Thanks for you point :slight_smile:


#10

Of course. We're here to make each other better coders!


#11

HELP!!! My code below returns the correct answers for each scenario, but I keep getting the error message saying it returns false if number=3... What does it mean?!

var isEven = function(number){
if (number%2 === 0){
return "This is an even number, good stuff!";
}
else if (number%2 >= 1) {
return "This is not an even number...";
}
else{
return "This is not even a number!";
}
};

var num = prompt("Type a number!");

isEven(num);


#12

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