• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

171
Views
Usando un bucle for en javascript con una función onclick para imprimir números del 1 al 10

Soy muy nuevo en todo esto y necesito crear un bucle for de JavaScript que imprima los números del 1 al 10 cuando se hace clic en un botón y de -1 a -10 cuando se hace clic en otro botón, como se muestra en la captura de pantalla adjunta.

captura de pantalla

He hecho esto pero estoy muy atascado.

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

almost 3 years ago · Santiago Trujillo
2 answers
Answer question

0

No sé si desea agregar un número cada vez que hace clic o agregarlos todos con un solo clic, pero hice esto:

 <!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>
almost 3 years ago · Santiago Trujillo Report

0

Prueba esto

 <!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>
almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error