我是JS的新手,这是我的第一门逻辑编程语言。出于某种原因,无论我的收入和贷款金额是多少,否则都会返回else console.log。我无法显示“已批准”消息吗?
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"
最佳答案
您的submit
函数定义为function (income, loanAmount)
。由于您不向函数传递任何内容,因此两者均设置为undefined,并且undefined / undefined >= 2
的值为false
。
解决此问题所需要做的就是:
从函数声明中删除参数。
在函数中将所有出现的income
替换为this.income
(与loanAmount
相同),因此它知道在哪个范围中查找它们。