I would like to know how to round up a year to the next World Cup year, like 2024 -> 2026, 2026->2026, or 2031 -> 2034
I really can't figure out the formula, but I guess I should use the %
operator?
My attempt:
let worldCupYear;
const remain = thisYear % 4;
if(remain === 0){
worldCupYear = 2+ thisYear
}else if(remain != 2){
worldCupYear = remain + thisYear;
}else{
worldCupYear = thisYear;
}
The year is every 4 years since 1930 which gives 2 when we use modulo 4
We need more code if you want to skip 2nd world war
const nextWorldCupYear = (thisYear) => {
const remainder = thisYear % 4;
// Calculate how many years to add to get to the next World Cup year (where year % 4 === 2)
const yearsToAdd = (4 + 2 - remainder) % 4;
return thisYear + yearsToAdd;
}
console.log(nextWorldCupYear(1930)); // Output: 1930
console.log(nextWorldCupYear(2024)); // Output: 2026
console.log(nextWorldCupYear(2030)); // Output: 2030
console.log(nextWorldCupYear(2031)); // Output: 2034