简体   繁体   中英

Trying to make times table calculator in javascript and html

I am trying to make a calculator in JavaScript and html so that when a number is entered and calculate is pressed the times table for that number will be displayed however nothing is being outputted. Here is my code any help is greatly appreciated. Many thanks for looking.

<html>

<head>
    <title> Javascript</title>
</head>

<body>
    <script>
        var i = 1;
        var result;
        var results = new Array();


        function calculate_this() {
            var input = document.getElementById("input").value;

            do {
                var result = input * i;
                results.push(result);
                i = i + 1;
            }
            while (i < 12);
            document.getElementById("output").innerHTML = results.string();

        }
    </script>

    <H1>Calculator</H1>
    <input type="text" id="input">
    <input type=button value="Calculate" onClick="calculate_this()">
    <br>
    <H2 id="output"></H2>


</body>

</html>

document.getElementById("output").innerHTML = results.toString();

.string()不是数组方法,所以它抛出了一个错误。

There is no Array method string() ... There is a method toString . So this line:

document.getElementById("output").innerHTML = results.string();

can be updated to this:

document.getElementById("output").innerHTML = results.toString();

 var i = 1; var result; var results = new Array(); function calculate_this() { var input = document.getElementById("input").value; do { var result = input * i; results.push(result); i = i + 1; } while (i < 12); document.getElementById("output").innerHTML = results.toString(); }
 <H1>Calculator</H1> <input type="text" id="input"> <input type=button value="Calculate" onClick="calculate_this()"> <br> <H2 id="output"></H2>

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