使用for-loop或while-loop从用户输入多个x数字?

时间:2021-05-28 21:22:48

I have 2-column table. 1st row from the left table show label "Enter X (Number)" and input field type="number" next to it. 2nd row "Show X" for how much Multiple X will be displayed, and the 3rd row for output/result.

我有2列表。左表的第1行显示标签“输入X(数字)”,并在其旁边输入字段类型=“数字”。第二行“显示X”表示将显示多个X,输出/结果的第三行。

Example:

User-Input

  • Enter X(number): 10
  • 输入X(数字):10

  • Show X : 7
  • 显示X:7

Result (output)

10, 20, 30, 40, 50, 60, 70

10,20,30,40,50,60,70

how to do this with for-loop? how to do this using while-loop with the same result?

如何用for-loop做到这一点?如何使用while循环以相同的结果执行此操作?

1 个解决方案

#1


0  

<html>
<head>
<script>
function showValues() {
    var num1 = document.getElementById("input1").value;
    var num2 = document.getElementById("input2").value;
    var outputString = "" + (num1);
    for (var count = 2; count <= num2; count++) {
       outputString += ", " + (num1 * count); 
    }
    document.getElementById("theAnswer").value = outputString;
    }
</script>
</head>
<body>
<div>
<table id="tableid">

<tr>
<td>Enter X(number):</td> <td><input id="input1"></td>
</tr>

<tr>
<td>Show X:</td> <td><input id="input2" ></td>
</tr>

<tr>
<td> answer </td> <td><input id="theAnswer" ></td>
</tr>
</table>
<br/>


<button type="button" onclick="showValues();">showValues<br/>
</div>
</body></html>

#1


0  

<html>
<head>
<script>
function showValues() {
    var num1 = document.getElementById("input1").value;
    var num2 = document.getElementById("input2").value;
    var outputString = "" + (num1);
    for (var count = 2; count <= num2; count++) {
       outputString += ", " + (num1 * count); 
    }
    document.getElementById("theAnswer").value = outputString;
    }
</script>
</head>
<body>
<div>
<table id="tableid">

<tr>
<td>Enter X(number):</td> <td><input id="input1"></td>
</tr>

<tr>
<td>Show X:</td> <td><input id="input2" ></td>
</tr>

<tr>
<td> answer </td> <td><input id="theAnswer" ></td>
</tr>
</table>
<br/>


<button type="button" onclick="showValues();">showValues<br/>
</div>
</body></html>