Skip to content

Commit f813ae4

Browse files
authored
Merge pull request #597 from Masa-Shin/fix-maths-is-divisible
Fix Maths/IsDivisible
2 parents 5c0c8b1 + 6d8b134 commit f813ae4

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

Maths/isDivisible.js renamed to Maths/IsDivisible.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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 TypeError('Expected a number')
66
}
77
if (num2 === 0) {
8-
return 'Not possible to divide by zero'
8+
return false
99
}
1010
return num1 % num2 === 0
1111
}

Maths/test/IsDivisible.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { isDivisible } from '../IsDivisible'
2+
3+
describe('isDivisible', () => {
4+
const testCases = [
5+
[0, 1, true],
6+
[0, 2, true],
7+
[1, 1, true],
8+
[1, 2, false],
9+
[2, 1, true],
10+
[4, 4, true],
11+
[16, 4, true],
12+
[36978235, 5, true],
13+
[36978235, 4, false],
14+
[4.5, 1.5, true],
15+
[4.5, 1.2, false],
16+
[5, 0, false],
17+
[5, -0, false]
18+
]
19+
20+
test.each(testCases)('if parameters are (%i, %i) it returns %p', (dividend, divisor, expected) => {
21+
expect(isDivisible(dividend, divisor)).toBe(expected)
22+
})
23+
24+
const errorCases = [
25+
[NaN, NaN],
26+
[NaN, 1],
27+
[1, NaN],
28+
['1', 1],
29+
[1, '1'],
30+
[1, true],
31+
[false, 2]
32+
]
33+
34+
test.each(errorCases)('throws an error if parameters are (%p, %p)', (dividend, divisor) => {
35+
expect(() => {
36+
isDivisible(dividend, divisor)
37+
}).toThrow()
38+
})
39+
})

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