javascript - Why does this do while only loop once?
阿神
阿神 2017-05-18 10:58:48
0
1
615
<!DOCTYPE html>
<html>
<head>
    <title>乘法表</title>
    <meta charset="utf-8">
    <script type="text/javascript">
        function multiX(x) {
            var str = "";
            for (var i = 1; i <=9; i++) {
                document.write(x+" * "+i+" = "+x*i+"</br>")
            }
        }
        var number1;
        do{
            number1 = parseFloat(prompt("please input a number",""));
            if (!isNaN(number1)) {
                multiX(number1);
            } else {
                alert("please input a number");
                continue;}
        } while (number1 == -1)
    </script>
</head>
<body>

</body>
</html>
阿神
阿神

闭关修行中......

reply all(1)
習慣沉默

First, what prompt()函数返回值,点取消返回null,点确定返回字符串信息。那么number1可能的值是null或是字符串。
然后,parseFloat() does is parse a string parameter and return a floating point number.

If the first character of the parameter string cannot be parsed into a number, parseFloat returns NaN.
And when the string parameter is null, NaN is also returned. Then number1 at this time is NaN.

The following if...else... does not make any changes to if...else...没有对number1进行任何改变。那么number1依然是NaN。
到了判断循环条件,while(number1 == -1)显然是当number1值为-1的时候循环才继续。
可见循环条件并不符合,所以do...while. Then

is still NaN. 🎜When it comes to judging the loop condition, while(number1 == -1) obviously the loop will continue when the 🎜 value is -1. 🎜It can be seen that the loop conditions are not met, so the do...while loop only runs once and ends. 🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template