Seeking a small method with odd Numbers

The user can create a new exam 200 questions in total.
The questions getting to be getting from the database. If the user wants 4 different booklets, the number 200 is divided into 4.

Thus, 50 questions are taken from each booklet but If the user chooses 3 booklets (200 / 3 = 66.66 – 66 * 3 = 198 – 2 remaining empty), there is a problem.

No problem in even numbers but I’m having trouble with odd numbers.
I have a small PHP code, but it doesn’t work correctly.
How can I make this scheme?

This is an incomplete code in JavaScript, that you can translate into PHP.

const totalQuestions = 200;
let totalBooklets = 3;
const questionsInEachBook = Math.floor(totalQuestions / totalBooklets);
const remainingQuestions = totalQuestions % totalBooklets
if (remainingQuestions > 0) {
  // (I assume each booklet is represented with an array?)
  // if the last booklet can have some extra remaining questions
  // then append the remaining questions to the last booklet

  // otherwise create another booklet containing remaining questions
  // and increment `totalBooklets` variable by 1

You could shorten it:

const remainingQuestions = totalQuestions % totalBooklets
1 Like

Thanks for the tip! :moneybag:

Thank you for your help!

1 Like