简体   繁体   English

非法的退货声明错误

[英]Illegal return statement error

What is wrong with my return statement? 我的退货单有什么问题?

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));

Your return statements are outside of any function. 您的return语句在任何函数之外。 You can only use return within a function. 您只能在函数内使用return

(You're also comparing a function with an integer, in if (creditCheck > 100) - did you mean to call the function there?) (您还正在将函数与整数进行比较, if (creditCheck > 100) -您是要在那里调用该函数吗?)

I have provided some clarification to your question below. 我在下面对您的问题做了一些澄清。 Hope it helps. 希望能帮助到你。

var income = 50; // Firstly, you need to declare income which I have set to 50 in this case// //首先,您需要申报本案中我设定为50的收入//

//You then need to declare creditCheck as a function of income. //然后您需要将creditCheck声明为收入的函数。 Note that, return only works within a function. 注意,return仅在函数内起作用。 To print to the console outside of a function, make use of console.log()// 要在功能之外打印到控制台,请使用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.//

//The text below shows what was printed to the console upon executing the function// //下面的文本显示了执行功能后打印到控制台的内容//

"Alas, you do not qualify for a credit card. Capitalism is cruel like that." “ A,你没有资格获得信用卡。资本主义是如此残酷。”

Re-indenting and simplifying your code reveals: 重新缩进并简化代码可以发现:

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`.

See comments - there's a lot wrong! 查看评论-有很多错误!

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

both of these returns are invalid because they are not within a function. 这两个返回值均无效,因为它们不在函数内。

(creditCheck > 100) is invalide because credicheck is a function and needs to be supplied with a variable to return anything (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.");
}

would add Alas, you do not qualify for a credit card. 会增加A,您不符合使用信用卡的条件。 Capitalism is cruel like that. 这样的资本主义是残酷的。 to the console log 到控制台日志

Please download http://www.helpmesh.net/s/JavaScript/ javascript.chm to get basic syntax for javascript and you will save yourself a lot of time. 下载http://www.helpmesh.net/s/JavaScript/ javascript.chm以获得javascript的基本语法,这样您可以节省很多时间。 The kind of problems, syntax, you are having is not what stackexchange was created for. 您所遇到的问题,语法不是为stackexchange创建的目的。

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

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