简体   繁体   English

Codecademy让我很难过,我做错了什么?

[英]Codecademy is giving me a hard time, what am I doing wrong?

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

It is telling me to: 它告诉我:

  1. Write a function named sleepCheck that takes the parameter numHours 编写一个名为sleepCheck的函数,它接受参数numHours
  2. Inside the function, write an if statement where if the number of hours of sleep is greater than or equal to 8, the computer will return "You're getting plenty of sleep! Maybe even too much!";. 在函数内部,写一个if语句,如果睡眠小时数大于或等于8,计算机将返回“你得到充足的睡眠!甚至可能太多!”;
  3. Otherwise (else) if the number of hours of sleep is less than 8, have the computer return "Get some more shut eye!"; 否则(否则)如果睡眠小时数小于8,请让计算机返回“多闭眼!”;

Then call the function with different hours of sleep 然后用不同的睡眠时间调用该函数

  1. Call the function with 10 hours of sleep, like this: sleepCheck(10); 用10小时的睡眠来调用这个函数,如下所示:sleepCheck(10);
  2. Call the function with 5 hours of sleep. 通过5小时的睡眠来调用该功能。
  3. Call the function with 8 hours of sleep. 在8小时的睡眠时调用该功能。

PS: I am a complete coding amateur, I just started Javascript two days ago. PS:我是一个完整的编码爱好者,我刚刚开始使用Javascript两天前。

Change else (numHours => 8) to just else . else (numHours => 8)更改为else

You're also missing some braces. 你也错过了一些牙套。

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

Notice how I matched my braces by having the closing brace at the same column as the statement with its corresponding opening brace. 请注意我是如何通过使闭合括号与具有相应左括号的语句在同一列上来匹配我的大括号。

If you think about it, this makes more sense anyway. 如果你考虑一下,无论如何这都更有意义。 else wouldn't make sense with a condition. else对某个条件没有意义。 It's a catch all. 这是一个很好的结果。 Its condition is everything else not caught by the if and else if blocks before it. 它的条件是其他所有未被ifelse if块捕获的条件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM