简体   繁体   English

Console.log返回错误的答案

[英]Console.log returns wrong answer

I'm new to JS, it's my first logical programming language. 我是JS的新手,这是我的第一个逻辑编程语言。 For some reason no matter what my income and loanAmount is the else console.log is returned. 出于某种原因,无论我的收入和loanAmount是什么,都会返回console.log。 What can't I get the "Approved" message to show? 有什么不能显示“已批准”的消息?

function LoanApp(personName, income, loanAmount){
    this.personName = personName;
    this.income = income;
    this.loanAmount = loanAmount;

    var bankBranch = "New York";
    var approvalStatus = "invalid";

    this.submit = function (income, loanAmount){
        if (income / loanAmount >= 2) {
            approvalStatus = "Approved";
            console.log("Congrats you're approved for " + this.loanAmount + ".")
        } else {
            approvalStatus = "On Review";
            console.log("Your application needs further review.")
        };
    };
};

var tony = new LoanApp("Tony"5000,1000);
tony.submit();  //returns else console.log but should be "Congrats"

Your submit function is defined as function (income, loanAmount) . 您的submit函数定义为function (income, loanAmount) Since you don't pass anything to the function, both are set to undefined, and undefined / undefined >= 2 evaluates to false . 由于您没有向函数传递任何内容,因此两者都设置为undefined, undefined / undefined >= 2计算结果为false

All you have to do to fix this is: 你需要做的就是解决这个问题:

  1. Remove the parameters from the function declaration. 从函数声明中删除参数。

  2. Replace all occurrences of income with this.income (and same with loanAmount ) in the function, so it knows what scope to look for them in. this.income (和loanAmount )替换所有出现的income ,因此它知道要查找它们的范围。

All you have to do is append the keyword this to the variables in your if then statement that determines if the value is >= 2. This will give the function access to the variables, otherwise it can't access them because they are out of the scope of the function. 您所要做的就是将关键字this附加到if then语句中的变量,以确定值是否> = 2.这将使函数访问变量,否则它无法访问它们,因为它们不在功能的范围。

Javascript: jsfiddle example - http://jsfiddle.net/larryjoelane/zrkdyq9c/6/ Javascript:jsfiddle示例 - http://jsfiddle.net/larryjoelane/zrkdyq9c/6/

function LoanApp(personName, income, loanAmount){
    this.personName = personName;
    this.income = income;
    this.loanAmount = loanAmount;

    var bankBranch = "New York";
    var approvalStatus = "invalid";

    this.submit = function (income, loanAmount){
        if ((this.income / this.loanAmount) >= 2) {  //<-----change that fixes code



            approvalStatus = "Approved";
            console.log("Congrats you're approved for " + this.loanAmount + ".")
        } else {


            approvalStatus = "On Review";
            console.log("Your application needs further review.")
        };
    };
};

var tony = new LoanApp("Tony",5000,1000);
tony.submit();  //returns else console.log but should be "Congrats"

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

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