-2

please help me, I am stuck. When I write it as

document.write(parseInt(n1) + ",")

It works, but when i write it as

document.getElementById("result1").innerHTML = parseInt(n1) + ",";

It does not work.

Instead of showing for example: 1,3,5,7,9,...

it only shows one number, like: 9,

Here's my code:

Maximum Range:<input type="number" id="maximum"> <br><br>

number 1: <input type="number" id="number1"> <br>
increment 1: <input type="number" id="increment1">

<br><br>



<button onclick="calculate();">Calculate</button> <hr>

<strong>Results:</strong>
<p id="result1"></p>

<script>
    function calculate() {
        let n1 = document.getElementById("number1").value;
        let i1 = document.getElementById("increment1").value;
        let max = document.getElementById("maximum").value;
        while (parseInt(n1) <= parseInt(max)) {
            document.getElementById("result1").innerHTML = parseInt(n1) + ",";
            n1 = parseInt(n1) + parseInt(i1);
        }
    }

</script>
2
  • 2
    innerHTML += ? Commented Nov 26, 2021 at 15:54
  • Wow I did not see it. Thank you very much! Commented Nov 26, 2021 at 15:56

1 Answer 1

0

change this line document.getElementById("result1").innerHTML = parseInt(n1) + ",";

to this

document.getElementById("result1").innerHTML += parseInt(n1) + ",";

as mentioned in comments..

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.