0

Explorer 忽略了我的代码中的循环并且对 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>
4

1 回答 1

1

当你来到这条线

while(c!=0 && id===true)

idundefined。所以循环永远不会开始。

假设其余代码是正确的,解决方案可能是将条件更改为

while(c!=0 && id!==false)

顺便说一句,即使您应该修复您的代码并了解它为什么当前不起作用,请注意有更简单的解决方案来测试字符串是否包含数字,例如正则表达式

var hasDigit = /\d/.test(x);
于 2013-11-08T10:25:33.943 回答