Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
Using a for loop in javascript with a onclick function to print numbers 1 to 10

I'm very new at all of this and I need to create a JavaScript for loop that prints out the numbers 1 to 10 when a button is clicked and -1 to -10 when another button is clicked as the attached screenshot shows.

screenshot

I've done this but I'm very stuck.

<!DOCTYPE html>
<html>

<body>

  <h1>For loop statement exercise</h1>

  <p>Press PLUS to display +1 to +10:
    <button onclick="plus()">PLUS</button>
    <p>Press MINUS to display -1 to -10:
      <button onclick="myFunction()">MINUS</button>
      <p id="i"></p>

      <script>
        for (i = 1; i <= 10; i++)

        {
          document.write("i" + < br > );
        }
      </script>

</body>

</html>

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I don't know if you want it to add a number every time you click or add them all in just one click, but I did this:

<!DOCTYPE html>
<html>
    <body>
        <h1>For loop statement exercise</h1>

        <p>Press PLUS to display +1 to +10:</p>
        <button onclick="plus()">PLUS</button>
        <p id="+"></p>

        <p>Press MINUS to display -1 to -10:</p>
        <button onclick="minus()">MINUS</button>
        <p id="-"></p>

        <script>
            function plus() {
                for(let i = 1; i <= 10; i++) {
                    document.getElementById("+").innerHTML += i + " ";
                }
            }

            function minus() {
                for(let i = -1; i >= -10; i--) {
                    document.getElementById("-").innerHTML += i + " ";
                }
            }
        </script>
    </body>
</html>
about 4 years ago · Santiago Trujillo Report

0

Try this

<!DOCTYPE html>
<html>
  <body>
    <h1>For loop statement exercise</h1>

    <p>
      Press PLUS to display +1 to +10: <button onclick="plus()">PLUS</button>
    </p>

    <p>
      Press MINUS to display -1 to -10:
      <button onclick="minus()">MINUS</button>
    </p>

    <p id="i"></p>

    <script>
      function minus() {
        for (i = -1; i >= -10; i--) {
          document.getElementById("i").innerHTML =
            document.getElementById("i").innerHTML + `${i} <br>`;
        }
      }
      function plus() {
        for (i = 1; i <= 10; i++) {
          document.getElementById("i").innerHTML =
            document.getElementById("i").innerHTML + `${i} <br>`;
        }
      }
    </script>
  </body>
</html>
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!