最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - codeacademy "Functions & ifelse" - Stack Overflow

programmeradmin2浏览0评论

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

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);
Share Improve this question asked Feb 8, 2014 at 18:31 user3287899user3287899 11 silver badge1 bronze badge
Add a ment  | 

7 Answers 7

Reset to default 5
  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

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

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 paring 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.

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

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);
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);
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.
发布评论

评论列表(0)

  1. 暂无评论