Skip to content

Commit 469a0e0

Browse files
committed
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.
1 parent 5a0db06 commit 469a0e0

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Maths/IsDivisible.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Checks if a number is divisible by another number.
22

3-
const isDivisible = (num1, num2) => {
4-
if (isNaN(num1) || isNaN(num2) || num1 == null || num2 == null) {
5-
return 'All parameters have to be numbers'
3+
export const isDivisible = (num1, num2) => {
4+
if (!Number.isFinite(num1) || !Number.isFinite(num2)) {
5+
throw new Error('All parameters have to be numbers')
66
}
77
if (num2 === 0) {
8-
return 'Not possible to divide by zero'
8+
return false
99
}
1010
return num1 % num2 === 0
1111
}
1212

1313
console.log(isDivisible(10, 5)) // returns true
1414
console.log(isDivisible(123498175, 5)) // returns true
15-
console.log(isDivisible(99, 5)) // returns false
15+
console.log(isDivisible(99, 5)) // returns false

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy