Demo: Combined Conditions Before FizzBuzz
Before you classify many numbers, classify one shipment count. % gives the remainder after division: 12 % 4 is 0, so 12 is divisible by 4. Check the combined case before either single case; otherwise a number divisible by both would stop at the first matching branch.
javascript
const cartons = 12;
if (cartons % 12 === 0) {
console.log("Pack in fours and threes");
} else if (cartons % 4 === 0) {
console.log("Pack in fours");
} else if (cartons % 3 === 0) {
console.log("Pack in threes");
} else {
console.log("Pack individually");
}
Run it, then try cartons = 8 and predict the branch before running again. In the next lesson, apply the combined-case-first rule to FizzBuzz; no loop is required yet.