If / else - help me


#1

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

the code below is worked , 1stly -- why i hve to use 'isNaN' ?. Even it is not in the instructon.

2ndly - kindly anybody explain the star marked line ?

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


#2

isNan is a method in Javascript used to check if a value is an integer or not. :slight_smile:


#3

want to know it according to instruction , if possible , kindly tell me .


#4

What do you mean, as in what it's doing in your code?


#5

i hve the correct code , bt want to understand something.

in the instruction isNaN is not given , why we use that ?


#6

You use it to check if the argument given in the function call is a number if I call and print out your function for example with a string,

console.log(isEven("hi"))

it should return,

"This is not a number"


#7

can't understand , would u tell me another way plz


#8

Sure :slight_smile: Okay imagine you want to call and print out your function isEven with the number 8 you would write,

console.log(isEven(8))

This will print out true to the intepreter because your if condition checks if a number is even and returns the boolean true if it is.

Now, if you were to call and print your function with a data type that is not an integer for example a string,

console.log(isEven("I am a string"))

Your code will return,

This is not a number

Because of your else if condition.

Otherwise your code will returnfalse if the argument called with your function is invalid.

I hope you understand clearly now. :grin:


#9

yap , thank you , now i understand


#10

Awesome! you're welcome. :slight_smile:


#11

last question -- kindly tell me this basic format about bracket ?

bracket within bracket .

(isNaN(number))


#12

Those are actually parentheses () and why they have to be to is because one is need for your else if condition so,

else if (condition)

and one is needed because isNan is a javascript method,

isNan(argument)

together,

else if (isNan(argument)) //Clsoses both the else if condition and JS method


#13

i got it , tnx 4 ur explanation ,

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

if i remove the last line 'isEven(6);' the code also works , how it works ?
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);


#14

anybody tell me plz .


#15

i think the program isn't checking to see if you called the function. it just wants to see that you wrote the "else if" statement correctly. :slight_smile:


#16

understand that , thank u , hope we will meet again :slight_smile:


#17

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


#18

is it worked ?............................................


#19

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