Help 2.If / else if / else


#1

What did i do wrong?

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


#2

First of all, try adding a semicolon after "Hi" and placing a space between return and "Hi". Secondly, in case that's not enough, please let me know what are the instructions of the exercise or at least what should be returned. Up to now, it should return true.


#3

Have you tried to refresh the page? What is the error message?


#4

What are you expecting as a result? The code as is works on my side.

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

First of all, make sure to close off your lines with semicolon (;) when writing JavaScript. Here, I've added it on the last line after (isEven(6)) and in the (else if) statement after (return "Hi").

The code gets the job done on my side - It returns true when you pass an even number to the function. It returns Hi for everything that isn't NaN.

Can you clarify what the task is and what the expected output is?


#5

im having the same problem. heres my code:
var isEven = function(number) {
if (number % 2 == 0) {
return true;
} else if (isNaN(number) = false) {
console.log('Please use an integer')
} else {
return false;
}
};
isEven(6);
heres the error:
Oops, try again.
It looks like you have a ReferenceError! Maybe you deleted isEven?


#6

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

isEven(6);

This is what worked for me. I don't think you have to set isNaN to false, because as long as its not a number, its going to do what you coded it to do.


#7

why did you make the % condition equal to 0?! that was the part i missed in my if/else and am not sure why it's needed. Thank you!


#8

% is the modulo operator and what it does is to compute the remainder of a division. Let's say you want to divide 9 by 4. This would be 2.25 for floating point operations but if you just use integer values it would be 9/4 = 2 and a remainder of 1. And as said modulo gives you the remainder. So if a number is divisible by another number the remainder is 0 and as even numbers are defined by being divisible by 2 that is exactly what we check for in this case, is this explanation ok or do you have any questions left?


#9

It makes sense, that's all. thank you !


#10

Hi all, I think I'm having a similar problem.

Here are the instructions: Add an else if branch to your existing if/else statement. If the number put into the function is not a number at all, instead of return true; or return false;, the function should return a string that tells the user that their input isn't a number. (This string can say whatever you like.)

Here's my code:

var isEven = function(number) {
// Your code goes here!
if (number % 2 === 0){
return "your number is even!";
}
else if (isNaN(number)===false){
return "please input a number";
}
else {
return "your number is odd!";
}
}

Here's the error: "Oops, try again. Your function returns true for 3!"

I don't know what I'm doing wrong :frowning:


#11

isNaN is the Question: Is the stuff in () not a number? And if it is a number the answer is false and otherwise it is true so if you check for false you block odd numbers but let strings go through.


#12

Hi hoaxer789, thank you so much for your help! I understand what you're saying, but even without isNaN it returns true for even numbers when it shouldn't. I went back to step one (which instructs me to create an if/else statement inside the isEven function and have it return true for even numbers) and used this code: var isEven = function(number) {
var number = Math.random();
Math.floor(number);
if (number % 2 === 0)
{
return true;
}
else
{
return false;
}
};
isEven(2);

The problem is that the code is returning false for the even number "2" when it should return true. I really have no idea when I've gone wrong ... :(.


#13

Well first of all you ignore the parameter by overwriting it with your own test value:

var number = Math.random();

So when the test function runs e.g. isEven(3) it expects false but your output depends on what Math.random throws out.

Also:

Math.floor(number);

as Math.random() generates a random number between 0 and really close to 1 and Math.floor rounds down to the nearest full integer number will always be 0. At least it would if the value computed here would be used ever after.

So as said number is a random number between 0 and very close to 1. If you now have a look at the digits after the decimal point and assume that the numbers are distributed uniformly than you probably could imagine that the chance of getting a 0 goes to 0 but this is the only case in which you get true as 0.5%2 = 0.5, 0.3%2 = 0.3 aso which leads to always getting false. So better get rid of these two lines:

var number = Math.random();
Math.floor(number);

and if you want to test it with random numbers what about this?

 var numberTest = Math.floor(Math.random()*1000);
console.log(numberTest);
console.log(isEven(numberTest));

#14

Solution this trouble:

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


#15

This makes so much sense! Thank you so much :D.


#16

To both of you (betacoder47141) and especially haxor789 since you've been helping me for days. I really appreciate it!


#17

In chrome it doesn't work =(
I passed this lesson only in Firefox


#18

well i changed the code to
var isEven = function(number) {
if (number % 2 == 0) {
return true;
} else if (isNaN(number)) {
console.log('Please use an integer')
} else {
return false;
}
};
isEven(6);
like i was told but its still not working
the warning says: "Oops, try again. Make sure your if/else if/else statement returns a string when the function input isn't a number." and the box says:
Please use an integer
true


#19

Aquiteus,
in your code, use Return "please use an integer" instead of console.log. also you are missing a ;

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

isEven(9);

the final isEven at the bottom outside of the braces is where you input your number or text and with this it will return either true, false, or state that it is not a number


#20

when the input number is not a number,isNaN() should be true,so the code should be as follows:
var isEven = function(number) {
// Your code goes here!
if(number%2===0){
return true;
}
else if(isNaN(number)===true){
return "The input is not a number!";
}
else{
return false;
}
};
isEven(6);