简体   繁体   中英

Multiple x number from user-input using for-loop or while-loop?

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.

Example:

User-Input

  • Enter X(number): 10
  • Show X : 7

Result (output)

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?

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM