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

268
Views
Cómo diseñar un bucle anidado si el límite no es múltiplo de los límites principal y secundario, sin usar el descanso

Tengo un código como el siguiente;

 let total = 15; let batch = 5; let batchRuns = 3; for (let i = 0; i < batchRuns; i++) { for(let j=0;j<batch;j++){ console.log("hello"); } console.log("-------------"); }

Ahora, esto imprimirá hello 15 veces desde 5x3=15. ¿Qué pasa si quiero imprimir hello 14 o 13 veces, manteniendo un lote como 5+5+4 o 5+5+3? Lo que quiero decir es que quiero imprimir guiones después de cada 5 hello y en la última iteración, si está por debajo de 5, también quiero imprimir una línea después de eso.

déjame mostrarte los resultados esperados

Caso 1

 total = 10; hello hello hello hello hello ------------- hello hello hello hello hello -------------

Caso 2

 total = 12; hello hello hello hello hello ------------- hello hello hello hello hello ------------- hello hello -------------
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Para las últimas batchRuns por lotes, calcule el batch y agregue guiones a la salida.

 let total = 12; let batch = 5; let batchRuns = 3; for (let i = 0; i < batchRuns; i++) { let currentBatch = i == batchRuns - 1 ? total % batch : batch; for (let j = 0; j < currentBatch; j++) { console.log("hello"); } console.log("-------------"); }

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!