Lesson 3 part 12


#1

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

my code works but i dont think i did it in the way the lesson wanted me to. i think it wants me to return those statements in the if/else rather than send them to the console but i cant figure out how to return them. for starters ig iwould have to put them in the function but after that im just confused.


#2

That line is the only one inside the function body, and not what we would expect. Remove the line and move the closing brace to the bottom of the code.


#3

ive tried this. there must be more to it than that because it still says sleepCheck isnt returning "your getting enough sleep..." when numHours=10


#4

Do you still have this line in your code? (It shouldn't be there.)

Write this after the function, as just a simple call. It doesn't return anything so the numHours will become undefined.

sleepCheck(5);

#5

yes indeed i do if i remove it, it doesn't change anything if i remove that line


#6

Post what you have for code, now, please, so we can start from there. Thanks.


#7

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


#8

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

i changed my code to this and now it appears thats its right if anyone has any advise on how to improve the code please tell me


#9

It's still not correct. What you had earlier was closer, sans the two errant lines of code.

... function (numHours) {
    if (...) {
        console.log(" ... ");
    } else {
        console.log(" ... ");
    }
};

#10

the program says that i am. when i click save and ssubmit it gives me a the Green button to go to the next lesson.


#11

That's the thing... Just because we get a green light does not mean the code is correct. It only contains enough of the expected elements to pass, with or without other errors. It's obvious that your code is still incorrect. If you choose not to believe that, then so be it.


#12

then whats wrong? you didnt say what was wrong only that what i had earlier was closer. earlier what i after looking back at it didnt use the function at all and didnt return the string instead it sent it to the console which is not what is expected. am i suposed to do both? does it not return the correct info?


#13

Before I can answer any further, I need to see the lesson. Please post a link. Thanks.


#14

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


#15

Okay, so it is return. Will have to try to remember that.

... function (numHours) {
    if (...) {
        return " ... ";
    } else {
        return " ... ";
    }
};

#16

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

So then would this be the correct answer?


#17

This is treating numHours as a function. That should be raising an exception or syntax error in the console.

Parentheses on return values only clutters up code, in my humble opinion. The values are already delimited with quotes. See my example above.

Also...

if the number of hours of sleep is greater than or equal to 8,


#18

if i remove the parentheses i get a SyntaxError. how do i do greater than or equal to? like this >==?


#19

I'll have to check if that operator even exists, but if it does, it would be correct assuming both values are the same type. Generally speaking, for inequality we normally use, >=, or <= when we know we are dealing with only numbers.


#20

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