Improve input validation of isDivisible

- Use Number.isFinite() for validation
- Throw an error instead of returning a string
- Instead of returning a string, return false when divisor === 0 since no numbers are in fact divisible by 0.
This commit is contained in:
Masahiko Shin
2021-05-05 14:22:10 +09:00
parent 5a0db06e8c
commit 469a0e06c8

View File

@ -1,11 +1,11 @@
// Checks if a number is divisible by another number. // Checks if a number is divisible by another number.
const isDivisible = (num1, num2) => { export const isDivisible = (num1, num2) => {
if (isNaN(num1) || isNaN(num2) || num1 == null || num2 == null) { if (!Number.isFinite(num1) || !Number.isFinite(num2)) {
return 'All parameters have to be numbers' throw new Error('All parameters have to be numbers')
} }
if (num2 === 0) { if (num2 === 0) {
return 'Not possible to divide by zero' return false
} }
return num1 % num2 === 0 return num1 % num2 === 0
} }