当我运行以下代码时:
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("75");
creditCheck("125");
creditCheck("100");
我明白了:
Alas you do not qualify for a credit card. Capitalism is cruel like that.
You earn a lot of money! You qualify for a credit card.
You earn a lot of money! You qualify for a credit card.
Alas you do not qualify for a credit card. Capitalism is cruel like that.
为什么变量没有给出正确的输出?
答案 0 :(得分:0)
不要使用
creditCheck("75");
相反,请使用
creditCheck(75);
使用引号,您将字符串传递给方法。你想传递一个数字。但是,在这种情况下,JavaScript无关紧要,因为JavaScript会自动转换它。你可能在你的问题中遗漏了一些东西;请发布完整的代码和输出。
答案 1 :(得分:0)
您在问题中发布的输出不是JavaScript控制台的输出,而是codeacademy.com editor的输出因此使我们无法理解哪个是问题。
你应该完全改变你的问题。但是,使用此代码可以正常工作。
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("75");
creditCheck("125");
creditCheck("100");