Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
How to design nested loop if the limit is not multiple of parent and child limits, without using break

I have a code like below;

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("-------------");
}

Now, this will print hello 15 times since 5x3=15. What if I want to print hello 14 times or 13 times, by keeping batch like 5+5+4 or 5+5+3. What I mean is, I want to print dashes after every 5 hellos and in the last iteration if it is below 5, I want to print line after that as well.

let me show the expected outputs

Case 1

total = 10;

hello
hello
hello
hello
hello
-------------
hello
hello
hello
hello
hello
-------------

Case 2

total = 12;

hello
hello
hello
hello
hello
-------------
hello
hello
hello
hello
hello
-------------
hello
hello
-------------
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For the last batchRuns, calculate the batch and add dashes to output.

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda