3 ms·
Hmmm, that's not what I'm seeing when all this is put together. isOdd coerces to a number using Math.abs, so the string checks in isNumber don't apply. fu
by aidos 2y ago
Hmmm, that's not what I'm seeing when all this is put together.
isOdd coerces to a number using Math.abs, so the string checks in isNumber don't apply.
function isOdd(value) {
const n = Math.abs(value);
if (!isNumber(n)) {
throw new TypeError('expected a number');
}
if (!Number.isInteger(n)) {
throw new Error('expected an integer');
}
if (!Number.isSafeInteger(n)) {
throw new Error('value exceeds maximum safe integer');
}
return (n % 2) === 1;
}
function isNumber(num) {
if (typeof num === 'number') {
return num - num === 0;
}
if (typeof num === 'string' && num.trim() !== '') {
return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
}
return false;
}