3

我正在做 Codecademy JS 培训,作为我即将学习的课程的回顾。我点击了 creditCheck 函数练习,它基本上只需要一个函数来计算一个整数并返回一个大于 100 的值,以及一个小于 100 的值。我认为下面的代码应该可以工作,但是它在没有被调用的情况下运行。为什么会这样?

creditCheck = function(income)
{
    income *= 1; // one way to convert the variable income into a number.
    if (income>100)
        {
            console.log("You earn a lot of money! You qualify for a credit card.");
            return true; // this is an actual return value, console.log() always returns "undefined"
        }
    else
        {
            console.log("Alas you do not qualify for a credit card. Capitalism is cruel like that.");
            return false;
        }
};
console.log("If the log is executing in the function def then this should print second. Otherwise it's probably being executed by the coding script itself.\n\n")

解决方案(可能):所以我只是在 Codecademy 站点的控制台中测试了脚本。它不会在除该站点之外的任何地方自行执行。这使我相信该页面发生了一些时髦的事情。

更多分辨率(也可能):我还添加了上面的最后一行来测试函数何时被调用。由于它是程序的最后一行,我的假设是,如果执行是在函数体本身中,那么最后一行将最后打印。这让我相信评分脚本会自行调用该函数,这意味着当我添加实际的函数调用时,它会搞砸。

4

5 回答 5

3

你必须调用你定义的函数。

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

    }; creditCheck(111);
于 2013-08-07T19:35:55.087 回答
0

正如其他回答者指出的那样,函数本身的声明中的语法有点不对劲,您需要调用该函数。下面是应该纠正这两个问题的代码。只需将此代码复制到一个新的 .html 文件中,然后在浏览器中打开:

 <html>
 <body>

 <h1>Credit Check Example</h1>
 <script>
 function creditcheck(income)
 {
  if (income>100)
    {
        alert("You earn a lot of money! You qualify for a credit card.");
    }
  else
    {
        alert("Alas you do not qualify for a credit card. Capitalism is cruel like that.");
    }

 }

 creditcheck(1000);
 </script>
 </body>
 </html>
于 2013-08-07T19:42:56.263 回答
0

实际的解决方案与我在提交的代码中调用我自己的函数以及评分函数解释这一点的方式有关。此外,脚本期待 console.log(#whatever) 的其他安排,因此当我不调用自己的函数时,这会使它变得复杂。

于 2013-09-19T17:55:59.727 回答
0

您必须调用该函数才能调用它;)

creditCheck = function(income)
{
    income *= 1; // one way to convert the variable income into a number.
    if (income>100)
        {
            console.log("You earn a lot of money! You qualify for a credit card.");
            return true; // this is an actual return value, console.log() always returns "undefined"
        }
    else
        {
            console.log("Alas you do not qualify for a credit card. Capitalism is cruel like that.");
            return false;
        }
};
var check = creditCheck(123); // runs the function
alert(check); // and alerts() the result
于 2013-08-07T19:39:21.583 回答
0

没有人被投票正确,所以我想我会对此进行排序。

问题是您需要重新阅读问题。如果大于或等于100。所以符号是'<=',而不是'<'。

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(75);
creditCheck(125);
creditCheck(100);
于 2013-09-12T10:04:02.560 回答