A proper solution would be along the lines of:
const isRealNumber = (n) => { if (typeof n !== 'number') return false; if (isNaN(n)) return false; if (n === +Inf || n === -Inf) return false; return true; }; const isEven = (n) => { if (!isNumber(n)) throw new Error("Numerical error: Invalid input"); return n % 2 === 0; } const isOdd = (n) => { if (!isNumber(n)) throw new Error("Numerical error: Invalid input"); return n % 2 !== 0; }
A proper solution would be along the lines of:
This is the additional complexity created by dynamically weakly typed languages. And that’s why we get all these BS tiny npm packages.