非法返回语句错误

时间:2013-11-13 22:09:56

标签: javascript if-statement return

我的退货声明有什么问题?

var creditCheck = function (income) {
    var val = income;
    return val;
};
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 个答案:

答案 0 :(得分:5)

您的return语句不属于任何函数。您只能在函数中使用return

(你还在if (creditCheck > 100)比较一个函数和一个整数 - 你的意思是在那里调用函数吗?)

答案 1 :(得分:1)

我已在下面的问题中提供了一些说明。希望它有所帮助。

var income = 50; //首先,你需要申报我在这种情况下设定为50的收入//

//然后,您需要将creditCheck声明为收入的函数。请注意,return仅适用于函数。要在函数外部打印到控制台,请使用console.log()//

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

creditCheck(income); //You can execute the function by calling it.//

//下面的文字显示了执行函数//

时打印到控制台的内容
  “唉,你没有资格获得信用卡。资本主义是残酷的   该“。

答案 2 :(得分:0)

重新缩进和简化代码显示:

var creditCheck = function(income) {
    return income; // essentially a no-op
};
if( creditCheck > 100) { // if a function is greater than a number?
    return "You earn a lot...";
    // is this code in a function? Otherwise it's an invalid return!
}
// else is unnecessary, due to `return` above.
return "Alas, you lack basic JavaScript knowledge...";
// console.log is never reached due to `return`.

看到评论 - 有很多错误!

答案 3 :(得分:0)

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

这两个返回都是无效的,因为它们不在函数中。

(creditCheck> 100)是无效的,因为credicheck是一个函数,需要提供一个变量才能返回任何内容

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

会添加Alas,您没有资格获得信用卡。资本主义是残酷的。到控制台日志

下载http://www.helpmesh.net/s/JavaScript/ javascript.chm以获取javascript的基本语法,您将节省大量时间。您遇到的问题,语法类型不是为其创建的堆栈交换。