确保每个左括号都有一个匹配的右括号很重要。
你的代码:
function creditCheck(income) {
var ans = 100;
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." );
}
格式应如下:
function creditCheck(income)
{
var ans = 100;
// I'm assuming that you mean to compare 'income' with 'ans'
// Notice that I replaced ';' with an opening bracket here
if(income>=ans)
{
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." );
}
// And here I've added a closing bracket
}
当您在由括号形成的条件块(例如)中只有一个操作时,if
您实际上不需要开始和结束括号。见下文。
//
function creditCheck(income)
{
var ans = 100;
// See, no more brackets here
if(income>=ans)
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." );
}