0

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

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

4 回答 4

5

您的return陈述在任何功能之外。您只能return在函数内使用。

(您还将一个函数与一个整数进行比较,在if (creditCheck > 100)- 您的意思是在那里调用该函数吗?)

于 2013-11-13T22:12:26.570 回答
1

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//

//You then need to declare creditCheck as a function of income. Note that, return only works within a function. To print to the console outside of a function, make use of 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."

于 2014-01-12T18:32:08.467 回答
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`.

查看评论 - 有很多错误!

于 2013-11-13T22:14:12.010 回答
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.");
}

会补充说,你没有资格获得信用卡。资本主义就是这么残酷。到控制台日志

下载http://www.helpmesh.net/s/JavaScript/javascript.chm以获取 javascript 的基本语法,您将节省大量时间。您遇到的问题和语法并不是创建 stackexchange 的目的。

于 2013-11-13T22:26:51.633 回答