最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Illegal return statement error - Stack Overflow

programmeradmin1浏览0评论

What is wrong with my return statement?

var creditCheck = function (ine) {
    var val = ine;
    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));

What is wrong with my return statement?

var creditCheck = function (ine) {
    var val = ine;
    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));
Share Improve this question edited Nov 13, 2013 at 22:12 Naftali 146k41 gold badges247 silver badges304 bronze badges asked Nov 13, 2013 at 22:09 Mike HawkMike Hawk 11 gold badge1 silver badge3 bronze badges 4
  • What is the exact exception message? – Justin Russo Commented Nov 13, 2013 at 22:11
  • 2 Your code seems horribly malformed. You create a function, then test if that function is greater than 100? – p.s.w.g Commented Nov 13, 2013 at 22:12
  • This is code academy. – Mike Hawk Commented Nov 13, 2013 at 22:13
  • This seems fishy: if (creditCheck > 100). It makes no sense to pare a function to a number. – Ted Hopp Commented Nov 13, 2013 at 22:14
Add a ment  | 

4 Answers 4

Reset to default 5

Your return statements are outside of any function. You can only use return within a function.

(You're also paring a function with an integer, in if (creditCheck > 100) - did you mean to call the function there?)

I have provided some clarification to your question below. Hope it helps.

var ine = 50; // Firstly, you need to declare ine which I have set to 50 in this case//

//You then need to declare creditCheck as a function of ine. 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 (ine) {
  if (ine > 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(ine); //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."

Re-indenting and simplifying your code reveals:

var creditCheck = function(ine) {
    return ine; // 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 ments - 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

var creditCheck = function (ine) {
    return ine;
};
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. Capitalism is cruel like that. to the console log

Please download http://www.helpmesh/s/JavaScript/ javascript.chm to get basic syntax for javascript and you will save yourself a lot of time. The kind of problems, syntax, you are having is not what stackexchange was created for.

发布评论

评论列表(0)

  1. 暂无评论