<!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>
習慣沉默2017-05-18 11:00:48
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. Thennumber1
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
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. 🎜