I'm also having troubles with this particular code


#1

I'm also having troubles with this particular 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!";
};
sleepCheck(10)
sleepCheck(5)
sleepCheck(8)
Anyone to help me out please.


JavaScript: Functions & if/else. I don't know why it is giving me an error message
#2

@olasky01
-1
For the Javascript Interpreter a semi-colon-; is an End-of-Statement indicator......
thus writing the IF ELSE statement like

if (numHours >= 8)
 { return "You're getting plenty of sleep! Maybe even too much!";
 };
else {
 return "Get some more shut eye!";
};

would lead to an ERROR else not expected
as for the Javascript Interpreter the statement would only be

if (numHours >= 8)
 { return "You're getting plenty of sleep! Maybe even too much!";
 }

-2
You are forgetting to use an extra closing-curly-bracket-}
with which you would close the FUNCTION-BODY.....

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

Please read the Function talk
and concentrate on
- parameter
- argument
- calling a function
- usage of the return statement

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

Remove the semicolon before else. It is sealing off your sleepCheck function.


#4

thanks. did the work successfully


#5

worked like magic. Thanks alot


#6

@olasky01

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html <---JavaScript code-convention
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript
http://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function
http://stackoverflow.com/questions/6260475/how-to-the-operator
http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1


#7

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