我正在尝试验证 JavaScript 主函数中的提示。基本上它要求输入 10 到 20 之间的数字。如果该值不在该范围内或不是数字,它应该给出错误消息并再次询问。如果输入 0 或按下取消按钮,它应该完全停止例程。
我知道这是非常糟糕的代码,但它非常适合我。
我遇到的问题是当我尝试在 JSLint 中验证它时出现以下错误:
Expected 'else { if' and instead saw 'else if'.
} else if (amtEntered >= 10 && amtEntered <=20) {
如果我取消注释 return 0
语句,错误就消失了。
var amt;
var inputError = false;
do {
if (inputError) {
alert ("You have entered an error. \n Please try again.");
}
amt = prompt("Enter amount between 10 and 20: ","");
amt = Number(amt);
if (isNaN(amt)) {
inputError = true;
} else if (amt === null || amt === 0) {
// If amt is blank or cancel button pressed then exit loop and exit function.
inputError = false;
return 0;
} else if (amt >= 10 && amt <=20) {
inputError = false;
} else {
inputError = true;
}
}
while (inputError);
如果有任何关于修复这段代码的提示或建议,我们将不胜感激。
最佳答案
好吧,您可以通过将 if
语句更改为:
if (isNaN(amt)) {
inputError = true;
} else {
if (amt === null || amt === 0) {
// If amt is blank or cancel button pressed then exit loop and exit function.
inputError = false;
return 0;
} else {
if (amt >= 10 && amt <=20) {
inputError = false;
} else {
inputError = true;
}
}
}
但我认为这只是 JSLint 的迂腐。您所拥有的实际上是有效的语法。
我相信 JSLint 提示这样的事情的原因可能是如果你有这样的代码:
if (condition)
do_something();
后来有人将其更改为:
if (condition)
do_something();
do_something_else();
如果没有大括号,该代码将无法按预期工作。您是否认为有足够充分的理由在您的代码中添加更多大括号主要是风格问题。
这种特殊情况之所以复杂,是因为它只在存在 return
语句时发生,因此推理更加复杂。
查看 source code ,它似乎加载了整个 if
block (真正的分支),然后检查下一个标记是否为 else
。如果 if
block 有干扰符(例如 return
),它会提示不必要的 else
(如果下一个标记是 else
) 或缺少大括号(如果下一个标记集是 else if
):
stmt('if', function () {
var paren = next_token;
one_space();
advance('(');
step_in('control');
no_space();
edge();
this.arity = 'statement';
this.first = expected_condition(expected_relation(expression(0)));
no_space();
step_out(')', paren);
one_space();
this.block = block('if');
if (next_token.id === 'else') {
if (this.block.disrupt) {
next_token.warn(this.elif ? 'use_nested_if' : 'unnecessary_else');
}
one_space();
advance('else');
one_space();
if (next_token.id === 'if') {
next_token.elif = true;
this.else = statement(true);
} else {
this.else = block('else');
}
if (this.else.disrupt && this.block.disrupt) {
this.disrupt = true;
}
}
return this;
});
unnecessary_else
很容易弄清楚,它是为了捕捉像这样的东西:
if (condition) {
return 0;
} else {
do_something();
}
在那种情况下,最好写成:
if (condition) {
return 0;
}
do_something();
use_nested_if
更有趣的地方在于,如果大括号是个好主意,则应该一直 使用它们,而不仅仅是当 block 有一个 时返回
语句。我不确定为什么作者认为这只是一个 block 返回时的问题。
如果它真的困扰您,您无疑可以修改代码以删除该检查,我不会轻易这样做。也许让作者在软件中添加tolerate_non_nested_if
选项可能会更好。或者,如果您知道它没问题,您可以忽略警告。
而且,经过进一步思考,这两个警告可能使用了相同推理 - 如果您有一个返回的 if
block ,那么之后的任何代码不应该在等效的 else
block 中,它应该是独立的(但当然在任何外部 if/else
block 的上下文中)。
我的意思是你可能会使用:
if (isNaN(amt)) {
inputError = true;
} else {
if (amt === null || amt === 0) {
// If amt is blank or cancel button pressed then exit loop and exit function.
inputError = false;
return 0;
} // no else needed here.
if (amt >= 10 && amt <=20) {
inputError = false;
} else {
inputError = true;
}
}
关于javascript - JSLint:预期为 'else { if' 而不是看到 'else if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25799728/