From 5a0db06e8c89112f0cad0560bdeb5135713baaa6 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Wed, 5 May 2021 14:21:20 +0900 Subject: [PATCH 1/5] Fix case of file name of isDivisible --- Maths/{isDivisible.js => IsDivisible.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Maths/{isDivisible.js => IsDivisible.js} (100%) diff --git a/Maths/isDivisible.js b/Maths/IsDivisible.js similarity index 100% rename from Maths/isDivisible.js rename to Maths/IsDivisible.js From 469a0e06c8317f0e1403ad6a264558735ef3f543 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Wed, 5 May 2021 14:22:10 +0900 Subject: [PATCH 2/5] 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. --- Maths/IsDivisible.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Maths/IsDivisible.js b/Maths/IsDivisible.js index 072957e920..1cdb6137cc 100644 --- a/Maths/IsDivisible.js +++ b/Maths/IsDivisible.js @@ -1,15 +1,15 @@ // 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 Error('All parameters have to be numbers') } if (num2 === 0) { - return 'Not possible to divide by zero' + return false } return num1 % num2 === 0 } console.log(isDivisible(10, 5)) // returns true console.log(isDivisible(123498175, 5)) // returns true -console.log(isDivisible(99, 5)) // returns false +console.log(isDivisible(99, 5)) // returns false \ No newline at end of file From cf5ff92ef984fa9658c991684d252e4e2f02ad18 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Wed, 5 May 2021 14:23:50 +0900 Subject: [PATCH 3/5] Add tests for Maths/IsDivisible --- Maths/test/IsDivisible.test.js | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Maths/test/IsDivisible.test.js diff --git a/Maths/test/IsDivisible.test.js b/Maths/test/IsDivisible.test.js new file mode 100644 index 0000000000..d4d4edc23a --- /dev/null +++ b/Maths/test/IsDivisible.test.js @@ -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() + }) +}) From a14da3d113998876927df2812b0de95d02493441 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Sun, 23 May 2021 17:44:15 +0900 Subject: [PATCH 4/5] improve error handling of isDivisible --- Maths/IsDivisible.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/IsDivisible.js b/Maths/IsDivisible.js index 1cdb6137cc..89ce0620e2 100644 --- a/Maths/IsDivisible.js +++ b/Maths/IsDivisible.js @@ -2,7 +2,7 @@ export const isDivisible = (num1, num2) => { if (!Number.isFinite(num1) || !Number.isFinite(num2)) { - throw new Error('All parameters have to be numbers') + throw new TypeError("Expected a number") } if (num2 === 0) { return false From 6d8b134ce7eebe464ed4b4d6ff077ed736355f13 Mon Sep 17 00:00:00 2001 From: Masahiko Shin Date: Sun, 23 May 2021 18:09:39 +0900 Subject: [PATCH 5/5] Formatted with standard --- Maths/IsDivisible.js | 4 ++-- Maths/test/IsDivisible.test.js | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Maths/IsDivisible.js b/Maths/IsDivisible.js index 89ce0620e2..9c2ec81cf5 100644 --- a/Maths/IsDivisible.js +++ b/Maths/IsDivisible.js @@ -2,7 +2,7 @@ export const isDivisible = (num1, num2) => { if (!Number.isFinite(num1) || !Number.isFinite(num2)) { - throw new TypeError("Expected a number") + throw new TypeError('Expected a number') } if (num2 === 0) { return false @@ -12,4 +12,4 @@ export const isDivisible = (num1, num2) => { console.log(isDivisible(10, 5)) // returns true console.log(isDivisible(123498175, 5)) // returns true -console.log(isDivisible(99, 5)) // returns false \ No newline at end of file +console.log(isDivisible(99, 5)) // returns false diff --git a/Maths/test/IsDivisible.test.js b/Maths/test/IsDivisible.test.js index d4d4edc23a..80ece0a0d7 100644 --- a/Maths/test/IsDivisible.test.js +++ b/Maths/test/IsDivisible.test.js @@ -17,21 +17,21 @@ describe('isDivisible', () => { [5, -0, false] ] - test.each(testCases)("if parameters are (%i, %i) it returns %p", (dividend, divisor, expected) => { - expect(isDivisible(dividend, divisor)).toBe(expected); + 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', 1], + [1, '1'], [1, true], [false, 2] ] - test.each(errorCases)("throws an error if parameters are (%p, %p)", (dividend, divisor) => { + 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