Reputation: 21406
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 hello
s 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
-------------
Upvotes: 0
Views: 31
Reputation: 22574
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("-------------");
}
Upvotes: 1