Skip to content

Fix Maths/IsDivisible #597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Maths/isDivisible.js → Maths/IsDivisible.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Checks if a number is divisible by another number.

const isDivisible = (num1, num2) => {
if (isNaN(num1) || isNaN(num2) || num1 == null || num2 == null) {
return 'All parameters have to be numbers'
export const isDivisible = (num1, num2) => {
if (!Number.isFinite(num1) || !Number.isFinite(num2)) {
throw new TypeError('Expected a number')
}
if (num2 === 0) {
return 'Not possible to divide by zero'
return false
}
return num1 % num2 === 0
}
Expand Down
39 changes: 39 additions & 0 deletions Maths/test/IsDivisible.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { isDivisible } from '../IsDivisible'

describe('isDivisible', () => {
const testCases = [
[0, 1, true],
[0, 2, true],
[1, 1, true],
[1, 2, false],
[2, 1, true],
[4, 4, true],
[16, 4, true],
[36978235, 5, true],
[36978235, 4, false],
[4.5, 1.5, true],
[4.5, 1.2, false],
[5, 0, false],
[5, -0, false]
]

test.each(testCases)('if parameters are (%i, %i) it returns %p', (dividend, divisor, expected) => {
expect(isDivisible(dividend, divisor)).toBe(expected)
})

const errorCases = [
[NaN, NaN],
[NaN, 1],
[1, NaN],
['1', 1],
[1, '1'],
[1, true],
[false, 2]
]

test.each(errorCases)('throws an error if parameters are (%p, %p)', (dividend, divisor) => {
expect(() => {
isDivisible(dividend, divisor)
}).toThrow()
})
})
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