2 ms·
Now someone is going to define const isOdd = (i) => !isEven(i); and you’ll get trouble. A proper solution would be along the lines of: const isRealNumber
by kuschku 4y ago
Now someone is going to define const isOdd = (i) => !isEven(i); and you’ll get trouble.
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;
}
This is the additional complexity created by dynamically weakly typed languages. And that’s why we get all these BS tiny npm packages.