SyntaxError:意外的令牌else(codecademy.com)

时间:2015-07-28 03:41:40

标签: javascript

我有一些问题是JS的新程序员。

它给了我一个错误:SyntaxError:意外的令牌

任何帮助都将受到赞赏!

以下是代码:

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

2 个答案:

答案 0 :(得分:0)

您错过了{

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

答案 1 :(得分:0)

在您的函数定义后{if (…)之后缺少}

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

此外,还有一些代码审查:

确保returnconsole.log一致。在一种情况下,您返回一个字符串,在另一种情况下,您记录一些东西。为两个案例做同样的事情。