Functions & if / else


#1

This is what i did:
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));

when i try to pass it says:

Oops, try again. There was a problem with your syntax.

Someone please help
Thanks in advance


#2

Below is my edited version of your code (copied and edited), I also put in some comments saying what I did.

var sleepCheck = function(numHours) { // Added an opening bracket here
    if (numHours<8) {
        return "You're getting plenty of sleep! Maybe even too much!";
    }
    else {
        return "Get some more shut eye!";
    }
}// Added an opening bracket here
console.log(sleepCheck(10));
console.log(sleepCheck(5));
console.log(sleepCheck(8));

Hope that helps!
- If you have any questions, feel free to ask!


#3

Its still not letting me pass
When i try to do it it says

Oops, try again. It looks like sleepCheck() isn't returning "You're getting plenty of sleep! Maybe even too much!" when numHours is 10. Check your if / else syntax and whether you're using the correct comparison operator.


#4

I see the problem.

if (numHours<8) {

should be

if (numHours>8) {

because it's supposed to return "You're getting plenty of sleep! Maybe even too much!" when you have more than 8 as the number.


#5

Thank you it worked
:smile::smile:


#6

No problem!  


#7

Thank you for posting the solution my code finally worked :smile:


#8

Hi @jibblyj ;

Thank you for the input on this post.

Please note I tried to upload an image of my screen to make it clearer but was not allowed to (haven't passed my trust level yet apparently :unamused:).

For some reason this solution will not work for me. Though the result window appears to be okay:

You're getting plenty of sleep! Maybe even too much!
Get some more shut eye!
You're getting plenty of sleep! Maybe even too much!

a pop-up message still comes up stating:

"Oops, try again. It looks like sleepCheck() isn't returning "You're getting plenty of sleep! Maybe even too much!" when numHours is 10. Check your if / else syntax and whether you're using the correct comparison operator."

My code is as follows (please note the >= changed to > makes no difference to the result):

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

Could you also explain why console.log was needed at the bottom. Or is that a silly question? Without it is nothing effectively happening - I realise console.log prints to the screen but what is effectively happening without this command? I only ask because in '9. Functions with two parameters' this was not required:

var perimeterBox = function(length, width) {
    return length + length + width + width;
};

perimeterBox(4, 23);

What is the difference?

Thanks for any help you can provide.


#9

Console.log is purely to print the answer, and without it everything else keeps happening anyway. I can't see a problem with the code, @stetim94, what about you?


#10

Your sleepcheck function is absolutely fine, maybe you hit a glitch? You could try a different browser as workaround.

what you call a screen, is really called a console, which explain why you use console.log (log something to the console). But for some reason i haven't yet fully figured out, codecademy will automatically log the last return statement to the console. Since in the case of your perimeterBox you only have on return statement, it will be automatically logged to the console. However, if you where to have two function calls:

var perimeterBox = function(length, width) {
    return length + length + width + width;
};

perimeterBox(4, 23);
perimeterBox(6, 12);

As you will see, the last function call gets logged to the console, not both. That is why in sleepCheck you use console.log, so all 3 function calls gets logged in the console, hope this helps


#11

@jibblyj Thanks for the input and bringing @stetim94 into the post. Much appreciated.
@stetim94 Thanks for the explanation of console.log and how codecademy displays some answers without console.log being used. Really helped clarify this point to me. I tried logging in with IE (instead of Chrome) but same issue with the pop-up message.

What other browsers do you have installed? Are there often glitches on codecademy?


#12

You're welcome. uhm.. the only thing i could think of is to include a space after your return statement:

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

#13

Can someone help me? After going through the lessons and even checking the boards i ended up with something like this

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

but i keep getting
Syntax Error: Unexpected token

what am i doing wrong?


#14

the closing curly brackets of if and else shouldn't have any semi-colons. And most important, this line:

var sleepCheck = function(numHours); {

shouldn't have any semi-colons


#15

var sleepCheck = function(numHours); {
    if (numHours>=8){
        return "You're getting plenty of sleep! Maybe even too much!";
    }; //Remove this semicolon.
    else {
        return "Get some more shut eye!";
    };//And this one.
}//Put a semicolon here.
console.log(sleepCheck (10)); //Remove the spaces between 'sleepcheck' and '(x)' on all of these.
console.log(sleepCheck (5));
console.log(sleepCheck(8));

#16

Thanks for the suggestion, but unfortunately this doesn't change the result. Hopefully just a glitch then.


#17

Could we seen an updated version of your code?


#18

Hi @stetim94, if you're directing that at me, here it is. Purely because you asked for this just now (if in fact you were referring to me rather than @bitcoder01653 's issue) I have just figured out the issue. So either way thanks.
It turns out that my OCD of ensuring the word 'much' (seen below with spaces between it and the previous word 'too' was the issue. Due to (perhaps the size of my laptop screen (10.1") this returned statement was on two lines rather than one line. For this reason to make it look more aesthetically pleasing I indented the word 'much' to line up directly below the start of the statement. This was causing the issue. So actually not a coding problem but rather not fitting the exact layout of spaces and words with codecademy solution:

var sleepCheck = function(numHours) {
    if (numHours >= 8) {    
        return "You're getting plenty of sleep! Maybe even too            much!";   //this irregular spacing!!
    } else {
        return "Get some more shut eye!";
    };
}
console.log(sleepCheck(10));
console.log(sleepCheck(5));
console.log(sleepCheck(8));

#19

Hey I did the course and passed but I wanted to find out why we have to add another bracket before calling the function??


#20

var sleepCheck = function(numHours) { /* function opening bracket */
    if (numHours >= 8) {    
        return "You're getting plenty of sleep! Maybe even too            much!";   //this irregular spacing!!
    } else {
        return "Get some more shut eye!";
    } /* closes the else */
} /* closes the function */

See comments in the code, it closes the function. You don't want to call the function, from inside the function!!