If/Else if/Else More on Control Flow


#1

Here is my code which was deemed correct:

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

My question:

Perhaps this was discussed in an earlier lesson and I forgot, but is "number" even defined? How does JavaScript know that "number" actually equals a number? Is "number" one of those words that the program simply knows?


#2

@michael_dhw,
-1
The return statement does NOT need curly-brackets-{ }
-2

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


#3

@michael_dhw,

++++ function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

#4

When you call the function, you will give the parameter a value so the function will know what 'number' is.

For example, to call your function, you will use (after you write your function):

var isEven = function(number) {if(number % 2 === 0) { return(true); }
else if(isNaN(number) === true) { return ("not a number"); }
else { return(false); }
};
isEven(number); <---- number is something that you can replace with whatever you want.

For example:

isEven(5); will return false
isEven(4); will return true
isEven("Horses"); will return "not a number" because "Horses" is not a number, it is a string.


#5

Thank you. I appreciate your responses.


#6

Here is my code, I keep having this error message : SyntaxError: Unexpected token else

Can't get passed this, any help, please.

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


#7

@devjumper04511,
The IF ELSE-IF ELSE construct has strict rules...

if ( condition ) {}
else {}

or

if ( condition ) {}
else if ( condition ) {}
else {}

or

if ( condition ) {}
else if ( condition ) {}
else if ( condition ) {}
else {}

#8


not having the same luck trying the code that way


#9


This one worked yet I'm not able to enter a string without an error either


#10

@leonhard_wettengmx_n i also tried it another will take you


#11

@leonhard_wettengmx_n this works yet placing any string value in causes an error


#12

Well as the error says hores is the name of a variable that has not been declared yet. Better use a value or a defined variable instead.