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

71
Views
Bucle de Javascript basado en la entrada

Tengo un caso: si el usuario ingresa 3 , entonces la salida debe ser 1 2 start .
Si el usuario ingresa 5 , entonces la salida debe ser 1 2 3 4 start .
Si el usuario ingresa más de 9 , entonces la salida debe ser Error .

Probé el siguiente código, pero no tengo idea de cómo puedo agregar el último número a la cadena

 <!DOCTYPE html> <html> <body> <p id="demo"></p> <script> let text = ""; for (let i = 1; i < 5; i++) { if (i == 5) break; text += i + "<br>"; } document.getElementById("demo").innerHTML = text; </script> </body> </html>

¿Alguna idea, por favor?
Muy apreciado.

Gracias.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Envuélvete en bucle for en una función. Luego puede concatenar los números y agregar la palabra start al regresar.

 const input = 15; console.log(countdown(input)) function countdown(input) { if (input > 9) { return 'Error!'; } c = ''; for (let i = 1; i < input; i++ ) { c += ' ' + i; } return c + ' start'; }

En tu caso

 const input = 5; document.getElementById("demo").innerHTML = countdown(input); function countdown(input) { if (input > 9) { return 'Error!'; } c = ''; for (let i = 1; i < input; i++ ) { c += ' ' + i + '<br/>'; } return c + ' start'; }
 <div id="demo"></div>

about 4 years ago · Juan Pablo Isaza Report

0

Aquí está mi respuesta:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Stack11</title> </head> <body> <p id="demo"></p> <input type="number" min="1" id="myInput" value="1"> <button onclick="getResult()"> GetResult </button> <script> let text = ""; function getResult(){ document.getElementById("demo").innerText = ""; let myInput = document.getElementById("myInput").value; myInput = parseInt(myInput); // From 1 to 9 not included 9 if (myInput < 9 && myInput > 0){ // You are excluding 1 from the input: 3 => 1, 2 : 5 => 1, 2, 3, 4 for (let i=1; i < myInput; i++){ text += i + "<br>"; } }else{ text = 'Output: The use input is wrong.'; } document.getElementById("demo").innerHTML = text; } </script> </body> </html>
about 4 years ago · Juan Pablo Isaza 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!