user3287899
user3287899

Reputation: 1

codeacademy "Functions & if/else"

I can't seem to find an answer to why this returns an error. The other questions regarding this section of code academy had to do with missing or misplaced "{}" and missing or misplaced ";". I don't believe I am having that problem, but it is still returning an error.

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

Upvotes: 0

Views: 6060

Answers (7)

Seth Bergman
Seth Bergman

Reputation: 426

This is the way I would tackle it. I am working in my local editor and console, so this example will give you the needed feedback when calling the function. It should satisfy the Codeacademy parameters.

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

console.log(sleepCheck(10));
console.log(sleepCheck(5));
console.log(sleepCheck(8));

This examle is using an inline conditional ternery operator. Not all cases call for them but sometimes you will find great uses for them. Using function

Upvotes: 1

goku999
goku999

Reputation: 1

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

// this works now works on code academy as I added the correct symbol on if  numHours. however when copied and pasted make sure you get rid of anything before the first line of the actual code which starts with var. this sometimes appears in codeacademy when you paste the code.

Upvotes: 0

Jane Zangs
Jane Zangs

Reputation: 31

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

Explanation: 1>you should be comparing the variable numHours not the function named sleepCheck.

for example:

var time = function(number){
    return number*2
};

var newNumber = time(10);

console.log(newNumber); 

Will print out 20

So in this case, nobody will use "return time*2", it does not make sense. Instead, they use "return number*2". The same idea as if (numHours>=8) not if (sleepCheck>=8)

2> is not "if (sleepCheck>8)", should be " if (numHours>=8)".

Hope I'm able to help you understand now.

Upvotes: 1

Stas Shklovsky
Stas Shklovsky

Reputation: 1

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

Upvotes: 0

rahuljaincse
rahuljaincse

Reputation: 1

sleepCheck is function

  var sleepCheck = function (hrs)

     {

     if (hrs >= 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);

Upvotes: 0

transcranial
transcranial

Reputation: 381

In your if statement, you should be comparing the variable numHours, not the function name sleepCheck as you are doing now.

Upvotes: 2

Satpal
Satpal

Reputation: 133433

  1. sleepCheck is a function you are using it as a variable.
  2. You need to use numHours parameter passed to function in if condition

Code

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

DEMO

Upvotes: 5

Related Questions