mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
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:
@ -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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user