0

无法弄清楚我的 if/else 子句有什么问题。不断给我一个意外的令牌错误。

var creditCheck = function(income) {
    var val = income;
};

if (creditCheck > 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.";
}

console.log(creditCheck(75));
4

3 回答 3

6

您的函数 creditCheck 不返回值。您必须将其退回才能按您的意愿工作:

var creditCheck = function(income)
{
    var val = income;
    return val;
};

然后你实际上必须使用括号来执行该函数:

var myScore = 750;
if (creditCheck(myScore) > 100) {
     //dosomething
} 
于 2013-11-13T21:27:14.397 回答
0

您意想不到的令牌在这里:

} else {
  ^

导致它成为意外令牌的错误在这里:

if (creditCheck > 100); {
                      ^

语句后有一个额外的分号if。这使得您的if语句后面跟着一个以分号结尾的空语句。这使得if语句后面的括号形成一个代码块,它本身是有效的。在代码块之后,else它本身是无效的。


如果您希望代码有意义,您应该if在函数中包含语句,并在条件中使用参数而不是函数名称:

var creditCheck = function(income) {
  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-11-13T21:30:45.547 回答
0

不确定这是否是您要执行的操作:

var creditCheck = function (income) {
    var val = income;
    if (val > 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-11-13T21:34:57.763 回答