<!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>
First, what
prompt()
函数返回值,点取消返回null
,点确定返回字符串信息。那么number1
可能的值是null
或是字符串。然后,
parseFloat()
does is parse a string parameter and return a floating point number.The following
is still NaN. 🎜When it comes to judging the loop condition,if...else...
does not make any changes toif...else...
没有对number1
进行任何改变。那么number1
依然是NaN。到了判断循环条件,
while(number1 == -1)
显然是当number1
值为-1的时候循环才继续。可见循环条件并不符合,所以
do...while
. Thenwhile(number1 == -1)
obviously the loop will continue when the 🎜 value is -1. 🎜It can be seen that the loop conditions are not met, so thedo...while
loop only runs once and ends. 🎜