12. Functions & if/else


#1


https://www.codecademy.com/courses/javascript-beginner-en-6LzGd/2/4?curriculum_id=506324b3a7dffd00020bf661


SyntaxError: Unexpected token else
Oops, try again. There was a problem with your syntax.

// Write your function below.
// Don't forget to call your function!
var sleepCheck = function(numHours){
if (numHours >= 8)
return "You're getting plenty of sleep! Maybe even too much!";
}
else {
return "Get some more shut eye!";
};
sleepCheck(10)
sleepcheck(5)
sleepCheck(8)
// What is wrong with the token else?
//What am I doing wrong?

`


#2

@gloryousmc,
You are forgetting an opening curly-bracket-{ after your
if (numHours >= 8)

and as you are using the return statement
you will have to use
console.log( sleepCheck(10) );
to get a displayed result

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".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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 ) );

#3

Ola amigo, vocĂȘ esqueceu de abrir a chave { do seu if e fecha-la.

var sleepCheck=function(numHours){
if (numHours>=8){ //voce esqueceu de abrir a chave.
return "You're getting plenty of sleep! Maybe even too much!";
}else {

return "Get some more shut eye!";
} //voce esqueceu de fechar a chave.
}
sleepCheck(10);
sleepCheck(5);
sleepCheck(8);


#4

@misaca,
Either call the function like

var theResult = sleepCheck(10);
console.log( theResult );

or

console.log( sleepCheck(10) );

#5

Os dois jeito vai da certo.


#6

What am i missing here? T_T

// Write your function below.
// Don't forget to call your function!
var sleepCheck = function (numHours)
{
if(numHours >= 8)
{
return "you're getting plenty of sleep! Maybe even too much!";
}

    else
    {
        return "Get some more shut eye!";
    }
};

sleepCheck(10);
sleepCheck(5);
sleepCheck(8);

#7

I am having the same issue for over an hour. I truly don't see the mistake I am making. @leonhard.wettengmx.n you are very helpful, nevertheless, I am failing to make the connection. Any help is appreciated. Here is my code:

// Write your function below.
// Don't forget to call your function!
var sleepCheck = function(numHours){
if (numHours >= 8)
return "You're getting plenty of sleep! Maybe even too much!";
};
else
{
return "Get some more shut eye!";
};
console.log (sleepCheck(10));
console.log (sleepCheck(5));
console.log (sleepCheck(8));


#8

@microsurfer50585,
You will want to use the EXACT text's as are mentioned in the Instructions !!!


#9

@eddieharmon,
You have 3 problems
-1
You are forgetting an opening curly-bracket-{
with which you would open your IF code-block

if ( condition) {
  //IF codeblock
}

-2
For the Javascript interpreter the semi-colon-; is an End-of-Statement indicator.
If you are using the semi-colon-; in an IF ELSE statement structure it will kill the IF ELSE flow.
For instance

if ( condition ) {
   //IF codeblock
};
else {
  //ELSE code-block
}

your ELSE statement is NOT seen as being part of the defined IF ELSE statement !!!!

-3
You are forgetting 1 closing curly-bracket-} at the end-of-function
with which you would close the sleepCheck() FUNCTION-BODY.


#10

Thank you, The closing curly bracket at the end was a HUGE help! I also eliminated the semicolon as well. The first problem that you mentioned, I think I had in my code already. Should it not have been on the same line as my function? (line 3)


#11

@eddieharmon
Please read
http://javascript.crockford.com/code.html <== code convention


#12

Thank you @leonhard.wettengmx.n . You are awesome if no one has told you already!


#13

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