From dde3abfa3053c297c28185845a5a0af97d87a47b Mon Sep 17 00:00:00 2001 From: Anastasiia_Klymenko Date: Mon, 26 May 2025 14:09:24 +0300 Subject: [PATCH 01/10] Add tests for splitInteger function --- splitInteger.test.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 splitInteger.test.js diff --git a/splitInteger.test.js b/splitInteger.test.js new file mode 100644 index 00000000..84a5e2ce --- /dev/null +++ b/splitInteger.test.js @@ -0,0 +1,27 @@ +function splitInteger(value, numberOfParts) { + const base = Math.floor(value / numberOfParts); + const remainder = value % numberOfParts; + const result = Array(numberOfParts).fill(base); + for (let i = 0; i < remainder; i++) { + result[numberOfParts - 1 - i]++; + } + return result.sort((a, b) => a - b); +} + +describe('splitInteger', () => { + test('returns [8] when value is 8 and numberOfParts is 1', () => { + expect(splitInteger(8, 1)).toEqual([8]); + }); + + test('splits 6 into 2 equal parts', () => { + expect(splitInteger(6, 2)).toEqual([3, 3]); + }); + + test('splits 17 into 4 parts with min/max diff <= 1', () => { + expect(splitInteger(17, 4)).toEqual([4, 4, 4, 5]); + }); + + test('splits 32 into 6 parts with min/max diff <= 1', () => { + expect(splitInteger(32, 6)).toEqual([5, 5, 5, 5, 6, 6]); + }); +}); From 2189647e28c89ba9f910b3f7275d1fc0d1452907 Mon Sep 17 00:00:00 2001 From: Anastasiia_Klymenko Date: Mon, 26 May 2025 14:22:30 +0300 Subject: [PATCH 02/10] Add implementation of splitInteger function --- splitInteger.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 splitInteger.js diff --git a/splitInteger.js b/splitInteger.js new file mode 100644 index 00000000..9fdfda4e --- /dev/null +++ b/splitInteger.js @@ -0,0 +1,11 @@ +function splitInteger(value, numberOfParts) { + const base = Math.floor(value / numberOfParts); + const remainder = value % numberOfParts; + const result = Array(numberOfParts).fill(base); + for (let i = 0; i < remainder; i++) { + result[numberOfParts - 1 - i]++; + } + return result.sort((a, b) => a - b); +} + +module.exports = splitInteger; From 8d59c038fa828ecf4591572bb6555f0a490133fd Mon Sep 17 00:00:00 2001 From: Anastasiia_Klymenko Date: Mon, 26 May 2025 14:39:28 +0300 Subject: [PATCH 03/10] Update test cases for splitInteger --- splitInteger.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/splitInteger.test.js b/splitInteger.test.js index 84a5e2ce..70b089a4 100644 --- a/splitInteger.test.js +++ b/splitInteger.test.js @@ -1,3 +1,4 @@ +const splitInteger = require('./splitInteger'); function splitInteger(value, numberOfParts) { const base = Math.floor(value / numberOfParts); const remainder = value % numberOfParts; From d2408191d96edfa73c734283d719415229e4323f Mon Sep 17 00:00:00 2001 From: Anastasiia_Klymenko Date: Mon, 26 May 2025 14:46:30 +0300 Subject: [PATCH 04/10] Fix splitInteger.test.js: update import and tests --- splitInteger.test.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/splitInteger.test.js b/splitInteger.test.js index 70b089a4..ef9d651b 100644 --- a/splitInteger.test.js +++ b/splitInteger.test.js @@ -1,13 +1,4 @@ const splitInteger = require('./splitInteger'); -function splitInteger(value, numberOfParts) { - const base = Math.floor(value / numberOfParts); - const remainder = value % numberOfParts; - const result = Array(numberOfParts).fill(base); - for (let i = 0; i < remainder; i++) { - result[numberOfParts - 1 - i]++; - } - return result.sort((a, b) => a - b); -} describe('splitInteger', () => { test('returns [8] when value is 8 and numberOfParts is 1', () => { From 67027877b47a7c568aa0eecc462b02c2ffcf5ff4 Mon Sep 17 00:00:00 2001 From: Anastasiia_Klymenko Date: Mon, 26 May 2025 14:58:12 +0300 Subject: [PATCH 05/10] fix: disable ESLint no-unused-vars for splitInteger import --- splitInteger.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/splitInteger.test.js b/splitInteger.test.js index ef9d651b..cf2d764c 100644 --- a/splitInteger.test.js +++ b/splitInteger.test.js @@ -1,5 +1,7 @@ +// eslint-disable-next-line no-unused-vars const splitInteger = require('./splitInteger'); + describe('splitInteger', () => { test('returns [8] when value is 8 and numberOfParts is 1', () => { expect(splitInteger(8, 1)).toEqual([8]); From 95d30d394d854ec0238e70fd8d3e35563ab8e820 Mon Sep 17 00:00:00 2001 From: Anastasiia_Klymenko Date: Mon, 26 May 2025 15:04:18 +0300 Subject: [PATCH 06/10] fix: correct splitInteger tests and use the imported function --- splitInteger.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/splitInteger.test.js b/splitInteger.test.js index cf2d764c..0c8198e5 100644 --- a/splitInteger.test.js +++ b/splitInteger.test.js @@ -1,6 +1,6 @@ -// eslint-disable-next-line no-unused-vars -const splitInteger = require('./splitInteger'); +'use strict'; +const splitInteger = require('./splitInteger'); describe('splitInteger', () => { test('returns [8] when value is 8 and numberOfParts is 1', () => { From 5f573be3ad7c36b632c62d01ddcaf20e0e546806 Mon Sep 17 00:00:00 2001 From: Anastasiia_Klymenko Date: Mon, 26 May 2025 15:17:01 +0300 Subject: [PATCH 07/10] fix: cleaned up and fixed tests for splitInteger --- splitInteger.test.js | 75 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 16 deletions(-) diff --git a/splitInteger.test.js b/splitInteger.test.js index 0c8198e5..cf01407f 100644 --- a/splitInteger.test.js +++ b/splitInteger.test.js @@ -2,20 +2,63 @@ const splitInteger = require('./splitInteger'); -describe('splitInteger', () => { - test('returns [8] when value is 8 and numberOfParts is 1', () => { - expect(splitInteger(8, 1)).toEqual([8]); - }); - - test('splits 6 into 2 equal parts', () => { - expect(splitInteger(6, 2)).toEqual([3, 3]); - }); - - test('splits 17 into 4 parts with min/max diff <= 1', () => { - expect(splitInteger(17, 4)).toEqual([4, 4, 4, 5]); - }); - - test('splits 32 into 6 parts with min/max diff <= 1', () => { - expect(splitInteger(32, 6)).toEqual([5, 5, 5, 5, 6, 6]); - }); +test('should split a number into equal parts if value is divisible by numberOfParts', () => { + expect(splitInteger(6, 3)).toEqual([2, 2, 2]); + expect(splitInteger(12, 4)).toEqual([3, 3, 3, 3]); +}); + +test('should return a part equal to value when splitting into 1 part', () => { + expect(splitInteger(8, 1)).toEqual([8]); + expect(splitInteger(123, 1)).toEqual([123]); +}); + +test('should sort parts ascending if they are not equal', () => { + const result = splitInteger(17, 4); + + expect(result).toEqual([4, 4, 4, 5]); + expect(result).toEqual([...result].sort((a, b) => a - b)); +}); + +test('should add zeros if value < numberOfParts', () => { + expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]); + expect(splitInteger(0, 3)).toEqual([0, 0, 0]); + expect(splitInteger(1, 4)).toEqual([0, 0, 0, 1]); +}); + +test('should ensure the sum of parts equals the original value', () => { + const testCases = [ + [8, 1], + [6, 2], + [17, 4], + [32, 6], + [0, 3], + [3, 5], + [1, 4], + [100, 9], + ]; + + for (const [value, parts] of testCases) { + const result = splitInteger(value, parts); + const sum = result.reduce((a, b) => a + b, 0); + expect(sum).toBe(value); + } +}); + +test('should ensure the difference between max and min is at most 1', () => { + const testCases = [ + [17, 4], + [32, 6], + [3, 5], + [0, 3], + [1, 4], + [100, 9], + ]; + + for (const [value, parts] of testCases) { + const result = splitInteger(value, parts); + const min = Math.min(...result); + const max = Math.max(...result); + + expect(max - min).toBeLessThanOrEqual(1); + } }); From 863f2a253bf07700cb5edcb1d2156f761a6a7451 Mon Sep 17 00:00:00 2001 From: Anastasiia_Klymenko Date: Mon, 26 May 2025 15:23:22 +0300 Subject: [PATCH 08/10] fix: cleaned up and fixed tests for splitInteger --- splitInteger.test.js | 2 -- src/splitInteger.test.js | 55 +++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/splitInteger.test.js b/splitInteger.test.js index cf01407f..16ec19bf 100644 --- a/splitInteger.test.js +++ b/splitInteger.test.js @@ -14,7 +14,6 @@ test('should return a part equal to value when splitting into 1 part', () => { test('should sort parts ascending if they are not equal', () => { const result = splitInteger(17, 4); - expect(result).toEqual([4, 4, 4, 5]); expect(result).toEqual([...result].sort((a, b) => a - b)); }); @@ -58,7 +57,6 @@ test('should ensure the difference between max and min is at most 1', () => { const result = splitInteger(value, parts); const min = Math.min(...result); const max = Math.max(...result); - expect(max - min).toBeLessThanOrEqual(1); } }); diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index a610317d..16ec19bf 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -2,20 +2,61 @@ const splitInteger = require('./splitInteger'); -test(`should split a number into equal parts - if a value is divisible by a numberOfParts`, () => { - +test('should split a number into equal parts if value is divisible by numberOfParts', () => { + expect(splitInteger(6, 3)).toEqual([2, 2, 2]); + expect(splitInteger(12, 4)).toEqual([3, 3, 3, 3]); }); -test(`should return a part equals to a value - when splitting into 1 part`, () => { - +test('should return a part equal to value when splitting into 1 part', () => { + expect(splitInteger(8, 1)).toEqual([8]); + expect(splitInteger(123, 1)).toEqual([123]); }); test('should sort parts ascending if they are not equal', () => { - + const result = splitInteger(17, 4); + expect(result).toEqual([4, 4, 4, 5]); + expect(result).toEqual([...result].sort((a, b) => a - b)); }); test('should add zeros if value < numberOfParts', () => { + expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]); + expect(splitInteger(0, 3)).toEqual([0, 0, 0]); + expect(splitInteger(1, 4)).toEqual([0, 0, 0, 1]); +}); + +test('should ensure the sum of parts equals the original value', () => { + const testCases = [ + [8, 1], + [6, 2], + [17, 4], + [32, 6], + [0, 3], + [3, 5], + [1, 4], + [100, 9], + ]; + + for (const [value, parts] of testCases) { + const result = splitInteger(value, parts); + const sum = result.reduce((a, b) => a + b, 0); + expect(sum).toBe(value); + } +}); +test('should ensure the difference between max and min is at most 1', () => { + const testCases = [ + [17, 4], + [32, 6], + [3, 5], + [0, 3], + [1, 4], + [100, 9], + ]; + + for (const [value, parts] of testCases) { + const result = splitInteger(value, parts); + const min = Math.min(...result); + const max = Math.max(...result); + expect(max - min).toBeLessThanOrEqual(1); + } }); From 3c0f1b65ad4ea6388c14815cd15e3a737083fad8 Mon Sep 17 00:00:00 2001 From: Anastasiia_Klymenko Date: Mon, 26 May 2025 15:25:28 +0300 Subject: [PATCH 09/10] fix: cleaned up and fixed tests for splitInteger --- src/splitInteger.test.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index 16ec19bf..98be9414 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -2,15 +2,22 @@ const splitInteger = require('./splitInteger'); -test('should split a number into equal parts if value is divisible by numberOfParts', () => { - expect(splitInteger(6, 3)).toEqual([2, 2, 2]); - expect(splitInteger(12, 4)).toEqual([3, 3, 3, 3]); -}); +test( + 'should split a number into equal parts if value is divisible by ' + + 'numberOfParts', + () => { + expect(splitInteger(6, 3)).toEqual([2, 2, 2]); + expect(splitInteger(12, 4)).toEqual([3, 3, 3, 3]); + } +); -test('should return a part equal to value when splitting into 1 part', () => { - expect(splitInteger(8, 1)).toEqual([8]); - expect(splitInteger(123, 1)).toEqual([123]); -}); +test( + 'should return a part equal to value when splitting into 1 part', + () => { + expect(splitInteger(8, 1)).toEqual([8]); + expect(splitInteger(123, 1)).toEqual([123]); + } +); test('should sort parts ascending if they are not equal', () => { const result = splitInteger(17, 4); From 487d1b29fa6baa5022ef5d862589f161c0e0b45f Mon Sep 17 00:00:00 2001 From: Anastasiia_Klymenko Date: Tue, 27 May 2025 12:37:09 +0300 Subject: [PATCH 10/10] Add new file --- splitInteger.test.js | 62 -------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 splitInteger.test.js diff --git a/splitInteger.test.js b/splitInteger.test.js deleted file mode 100644 index 16ec19bf..00000000 --- a/splitInteger.test.js +++ /dev/null @@ -1,62 +0,0 @@ -'use strict'; - -const splitInteger = require('./splitInteger'); - -test('should split a number into equal parts if value is divisible by numberOfParts', () => { - expect(splitInteger(6, 3)).toEqual([2, 2, 2]); - expect(splitInteger(12, 4)).toEqual([3, 3, 3, 3]); -}); - -test('should return a part equal to value when splitting into 1 part', () => { - expect(splitInteger(8, 1)).toEqual([8]); - expect(splitInteger(123, 1)).toEqual([123]); -}); - -test('should sort parts ascending if they are not equal', () => { - const result = splitInteger(17, 4); - expect(result).toEqual([4, 4, 4, 5]); - expect(result).toEqual([...result].sort((a, b) => a - b)); -}); - -test('should add zeros if value < numberOfParts', () => { - expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]); - expect(splitInteger(0, 3)).toEqual([0, 0, 0]); - expect(splitInteger(1, 4)).toEqual([0, 0, 0, 1]); -}); - -test('should ensure the sum of parts equals the original value', () => { - const testCases = [ - [8, 1], - [6, 2], - [17, 4], - [32, 6], - [0, 3], - [3, 5], - [1, 4], - [100, 9], - ]; - - for (const [value, parts] of testCases) { - const result = splitInteger(value, parts); - const sum = result.reduce((a, b) => a + b, 0); - expect(sum).toBe(value); - } -}); - -test('should ensure the difference between max and min is at most 1', () => { - const testCases = [ - [17, 4], - [32, 6], - [3, 5], - [0, 3], - [1, 4], - [100, 9], - ]; - - for (const [value, parts] of testCases) { - const result = splitInteger(value, parts); - const min = Math.min(...result); - const max = Math.max(...result); - expect(max - min).toBeLessThanOrEqual(1); - } -});