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

279
Views
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 answers
Answer question

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