Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

278
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda