12/13 functions and if/else help pls


#1


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


SyntaxError: Illegal return statement


Replace this line with your code. 
var sleepCheck = numHours

if (sleepCheck(10) > 8) {
    return "You're getting plenty of sleep!";
}
else {
    return "Get some more shut eye!";
}


#2

What does return do, when may it be used, what effect were you looking for, does return match that effect?


#3

are u being a troll or am i an idiot


#4

He is trying to get you to understand your problem by making you think about your error and come to an educated conclusion on what your problem is. Instead of just telling you what your problem is he is looking for you to figure that out by pushing you in the right direction! :smiley:


#5

ok ill remove return


#6

No you need returns in your code as step 2 and 3 require returns. Think about what makes a return statement valid and look back over your code to see what you might need to add or change so that the return statement is valid.


#7

return only makes sense in a function
You don't have a function, ergo, return doesn't make sense -> syntax error
By finding out how return may be used, you can determine what is wrong about your use of return


#8

doesn't work for me too :-/

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


#9

You need a semi-colon after your if, than statement ends, and another after the entire function ends.


#10

Your code is correct but you need return string exactly how it is in instruction. So instead

return "You're getting plenty of sleep! Maybe even too much !";

should be

return "You're getting plenty of sleep! Maybe even too much!";

Difference is only 1 space before !.


@objectrunner95172, @nico1984 if/else statement doesn't need semi-colon but function expression does.

Syntax

if (condition) {
    block of code to be executed if the condition is true
}

if (condition) {
    block of code to be executed if the condition is true
} else { 
    block of code to be executed if the condition is false
}

if (condition1) {
    block of code to be executed if condition1 is true
} else if (condition2) {
    block of code to be executed if the condition1 is false and condition2 is true
} else {
    block of code to be executed if the condition1 is false and condition2 is false
}

function functionName (param1, param2, ...) {
    code to be executed
}

var myFunction = function functionName]  (param1, param2, ...) {
  code to be executed
};

#11

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