const quarterOf = (month) => {
return Math.floor(((month + 11) / 3) % 4) + 1;
};
Ldq approached this problem mathematically. Rather than selecting from a list of possible answers, they created a formula to calculate the correct value from the argument.
Math.floor(number
): This function rounds a floating point number down to
the nearest integer
parenthesis: As the normal order of operations would not work, ldq used parenthesis to group the mathematical operators differently.
+, /, %, +: these operators combine the argument with hard-coded values to calculate the correct solution.
This strategy could also be implemented using these (but not only these) Implementation ...
- an arrow function with implicit return
- bitwise operators (if you want to make your life difficult)
Math.ceil
to round up, instead of rounding down
For ldq's solution I needed to review operator precedence and look up how
Math.floor
works.
- operator precedence: scriptingmaster, Avelx, BOMDAS, GreeneMath
Math.floor
: MDN