>资源管理器忽略我的代码中的循环,对var id不做任何事情。我真的很困惑,因为我检查它应该告诉我什么时候有数字,什么时候没有。
<!DOCTYPE html>
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
var c;
var id;
var x=prompt("let´s see if this work.");
c =(x.length);
alert("have " + c + " characters ");
alert("Last character is " + x.substring((c-1), c));
// loop supposed to check ever character and stop when counter "c" reach 0.
//id is supposed to become false if there is a number inside and stop the loop
// for some reason firefox is skipping loop
while (c!=0 && id===true)
{
alert("start in " + c);
id=isNaN(x.substring((c-1), c));
c=(c-1);
alert("ends in " + c);
}
// id true mean no nomber id false mean there is a number
if (id === true)
{
alert("No number inside")
}
else
{
alert("number inside");
}
</script>
</body>
</html>
当你来到这一行时
while(c!=0 && id===true)
id
是undefined
.所以循环永远不会开始。
假设代码的其余部分是正确的,解决方案可能是将条件更改为
while(c!=0 && id!==false)
顺便说一句,即使你应该修复你的代码并理解为什么它目前不起作用,请注意,有更简单的解决方案来测试字符串是否包含数字,例如正则表达式:
var hasDigit = /d/.test(x);