我正在网上学习javascript,我被困在需要调用函数的地方
var creditCheck = function (income)
{
if (income>=100)
{
return console.log("You earn a lot of money! You qulaify for a credit card.");
}
else {
return console.log("Alas yo do not qualify for credit card. Capitalism is creul like that.");
}
};
creditCheck(75);
以上是我调用creditCheck
函数的代码。现在当我在函数参数else
命令中放入75时(即"Alas yo do not qualify for credit card. Capitalism is creul like that."
)在控制台中执行两次。
为什么这个文本在控制台中出现两次?
由于
答案 0 :(得分:3)
编辑:CodeAcademy会自动调用参数为75的函数,因此您无需调用该函数。对于你的代码,你想要返回一个字符串,所以不是返回console.log
,而是在console.log中返回字符串,比如
return "You earn a lot of money! You qulaify for a credit card."; //or whatever the string is
此代码将通过测试:
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.' ;
}
};
当我在我的控制台中运行它时,我只收到一条声明。我猜你在别的地方叫它。发布您的整个代码,否则很难帮助您。但是,当您说return console.log()
时,您每次都会返回undefined
,所以它没有任何用处。您可能想要做的是
var creditCheck = function (income)
{
if (income>=100)
{
console.log("You earn a lot of money! You qulaify for a credit card.");
return true; //or return "You earn a lot of money!"
}
else {
console.log("Alas yo do not qualify for credit card. Capitalism is creul like that.");
return false;//or return "You don't qualify";
}
};
creditCheck(75);