12 function/else ....it is driving me crazy


#1

i need help.. what is wrong with this code? refresh my memory

var sleepCheck = function (numHours) {
if(hours of sleep >=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);


#2

What makes you say there's anything wrong about it? Gotta.. present the problem if anyone's to help you with it. If you drop of the code and figure we can figure out what you didn't type out then.. meh
If you are to solve a problem you have to identify it, and you seem to have, so start by presenting how far you've gotten, and save the rest of us from repeat work / show that you'll do the part that you are able to do and only ask about where you're actually stuck. Ideally we'd only point you in the right direction when you don't know what to do next, or spot a problem in how you've gone about trying to solve it, so that you can backtrack and start over from there


#3

i am sorry if i am bothering you guys with my problem with this code...
when i clicked summit, it gives me
Oops, try again. There was a problem with your syntax.


#4

It's not that. I don't need to be here. But I'm not interested in fixing it for you, rather I'm interested in putting you in a position to fix it yourself. That's also what you need to be asking for if you want knowledgeable people to help you out, they have no interest in doing it for you, but may very well be willing to help out where you're stuck.

Syntax errors are usually trivial to find and fix. Typically they come with a line number where the code no longer makes sense. The actual mistake might be earlier than that. But that line number is a good place to start, and perhaps a bit more of a hint than just "syntax error". So what you'll want to do is to run your code in another interpreter that provide this. Your browser is almost certain to have a js interpreter (this forum would look terrible without js for example) so you may be able to run it with your browser. You can also find an online interpreter, or install nodejs locally.


#5

@reycuban,
Try it by replacing your
hours of sleep
with
numHours

The parameter numHours is used as a =local= variable
throughout your sleepCheck function.

Then as you call the function
like
sleepCheck(10);
you are providing the sleepCheck function with a number Value 10

Please read the function talk for the proper terminology

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

#6

3 posts were split to a new topic: It looks like sleepCheck() isn't returning


#7

Hi there, I think you forgot to put a semi-colon after your closing curly bracket of your function.

see below:

var sleepCheck = function(numHours) {
// some codes here.....

}:


#8

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