-2

我正在通过代码学院自学 javascript,并不断获得“意外的令牌”。我错过了什么:

function creditCheck(income) {
    var ans = 100; if(income>=100);     
    return("You earn a lot of money! You qualify for a credit card."); 
} 
else {     
    return("Alas you do not qualify for a credit card. Capitalism is cruel like that." ); 
} 

信用检查(75);
信用检查(125);
信用检查(100);

4

5 回答 5

1

JSLint 会抱怨给出的例子。它更喜欢:

function creditCheck(income) {
   var ans = 100; 
   if (income >= 100) {
       return("You earn a lot of money! You qualify for a credit card."); 
   } 
   return("Alas you do not qualify for a credit card. Capitalism is cruel like that."); 
}

我之所以提到这一点,是因为代码示例中的 else 如果只包含最终返回,则它是不必要的。

于 2013-10-01T17:22:15.463 回答
0

您的代码现在执行function creditcheck() { ...} else {...} A 函数没有else{}. 你在 if 上犯了一个小错误,这是正确的方法:

function creditCheck(income) {
    var ans = 100;
    if(income>=100){
        return("You earn a lot of money! You qualify for a credit card."); 
    } 
    else {     
        return("Alas you do not qualify for a credit card. Capitalism is cruel like that." ); 
    } 
}

或者我会使用的方法:

function creditCheck(income) {
    var ans = 100;
    var returnValue;
    if(income>=100){ returnValue = "You earn a lot of money! You qualify for a credit card."; } 
    else {           returnValue = "Alas you do not qualify for a credit card. Capitalism is cruel like that."; } 

    return returnValue; // Return it at the end. This way the action `return` only happends at the end
}

这对于调试来说更容易。如果要检查返回的值,只需在返回值console.log(returnValue);上方添加一行。

于 2013-10-01T17:06:48.927 回答
0

删除一个;并添加 {

function creditCheck(income) 
{
       var ans = 100; 
       if(income>=100)
       {
           return("You earn a lot of money! You qualify for a credit card."); 
       } 
       else
       {     
           return("Alas you do not qualify for a credit card. Capitalism is cruel like that."   ); 
       }
}

如果你不想放 { 你可以写..

function creditCheck(income) 
{
       var ans = 100; 
       if(income>=100)
           return("You earn a lot of money! You qualify for a credit card."); 
       else
           return("Alas you do not qualify for a credit card. Capitalism is cruel like that."   ); 
}
于 2013-10-01T17:08:57.720 回答
0

好吧,这就是您解决代码的方法:

function creditCheck(income) {
    var ans = 100; 
    if(income>=100){     
        return("You earn a lot of money! You qualify for a credit card."); 
    }else {     
        return("Alas you do not qualify for a credit card. Capitalism is cruel like that." ); 
    }
} 

else 应该在 if 语句之后

于 2013-10-01T17:09:47.207 回答
0

确保每个左括号都有一个匹配的右括号很重要。

你的代码:

function creditCheck(income) {
    var ans = 100;
    if(income>=100);
    return("You earn a lot of money! You qualify for a credit card."); 
    } 
    else {     
        return("Alas you do not qualify for a credit card. Capitalism is cruel like that." ); 
} 

格式应如下:

function creditCheck(income)
{
    var ans = 100;
    // I'm assuming that you mean to compare 'income' with 'ans'
    // Notice that I replaced ';' with an opening bracket here
    if(income>=ans) 
    {  
        return("You earn a lot of money! You qualify for a credit card."); 
    }
    else
    {     
        return("Alas you do not qualify for a credit card. Capitalism is cruel like that." );
    }
    // And here I've added a closing bracket
}

当您在由括号形成的条件块(例如)中只有一个操作时,if您实际上不需要开始和结束括号。见下文。

// 
function creditCheck(income)
{
    var ans = 100;
    // See, no more brackets here
    if(income>=ans) 
        return("You earn a lot of money! You qualify for a credit card."); 
    else
        return("Alas you do not qualify for a credit card. Capitalism is cruel like that." );
}
于 2013-10-01T17:09:58.680 回答