JavaScript 中的乘法 table - 2 table 秒

Multiplication table in JavaScript - 2 tables

我开始学习 javascript 并且我对这段代码有疑问。该代码要求输入两个数字(第一个数字必须小于第二个数字),然后显示从第一个数字到第二个数字的乘法。如果您键入 5 和 7,它会显示 5、6 和 7 的乘法。

当输入的第二个数字是 10 时就会出现问题(除非您输入 1 和 10,否则它会显示全部)。如果我输入 2 和 10,它什么都不显示。

提前致谢。

<script>
function multiply() {
do {
  do {
    var i1 = prompt("Type first number from 1 to 10", "");
  } while (i1 < 1 || i1 > 10);

  do {
    var i2 = prompt("Type second number from 1 to 10 (number must be higher than the first one", "");
  } while (i2 < 1 || i2 > 10);

  var check = i2 - i1;

  if (check >= 0) {
    for (var i = i1; i <= i2 ; i++) {
        for (var j = 1; j <= 10; j++) {
      document.write("<br>" + i + " x " + j + " = " +  i * j);
        }
    document.write("<p>" );
    }
  } else {
    alert("First number is higher than the second, PLease try again.")
    }

} while (check < 0)
} 
</script>

prompt ()的return-value是一个字符串。所以你需要parseInt()得到整数。

do {
  do {
    var i1 = parseInt (prompt("Type first number from 1 to 10", ""));
  } while (i1 < 1 || i1 > 10);

  do {
    var i2 = parseInt (prompt("Type second number from 1 to 10 (number must be higher than the first one", ""));
  } while (i2 < 1 || i2 > 10);

  var check = i2 - i1;

  if (check >= 0) {
    for (var i = i1; i <= i2 ; i++) {
        for (var j = 1; j <= 10; j++) {
      document.write("<br>" + i + " x " + j + " = " +  i * j);
        }
    document.write("<p>" );
    }
  } else {
    alert("First number is higher than the second, PLease try again.")
    }

} while (check < 0)