From 6f1ccba72cacefc8268677aab7815693ac0cc1cb Mon Sep 17 00:00:00 2001 From: Serhii-Khobotov Date: Fri, 5 Sep 2025 10:36:26 +0300 Subject: [PATCH 1/3] my solution --- src/splitInteger.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index a610317d..c1a51462 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -4,18 +4,18 @@ const splitInteger = require('./splitInteger'); test(`should split a number into equal parts if a value is divisible by a numberOfParts`, () => { - + expect(splitInteger(12, 3)).toEqual([4, 4, 4]); }); test(`should return a part equals to a value when splitting into 1 part`, () => { - + expect(splitInteger(7, 1)).toEqual([7]); }); test('should sort parts ascending if they are not equal', () => { - + expect(splitInteger(8, 3)).toEqual([2, 3, 3]); }); test('should add zeros if value < numberOfParts', () => { - + expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]); }); From 0d36b4fc3c2ad9fe52cc3df1c11ef408d49abf92 Mon Sep 17 00:00:00 2001 From: Serhii-Khobotov Date: Fri, 5 Sep 2025 11:54:37 +0300 Subject: [PATCH 2/3] added more tests --- src/splitInteger.js | 2 ++ src/splitInteger.test.js | 61 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/splitInteger.js b/src/splitInteger.js index d3da7485..b1388973 100644 --- a/src/splitInteger.js +++ b/src/splitInteger.js @@ -20,4 +20,6 @@ function splitInteger(value, numberOfParts) { return parts; } +// console.log(splitInteger(12, 0)); + module.exports = splitInteger; diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index c1a51462..bcbb713b 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -9,13 +9,72 @@ test(`should split a number into equal parts test(`should return a part equals to a value when splitting into 1 part`, () => { - expect(splitInteger(7, 1)).toEqual([7]); + expect(splitInteger(8, 1)).toEqual([8]); }); test('should sort parts ascending if they are not equal', () => { expect(splitInteger(8, 3)).toEqual([2, 3, 3]); + expect(splitInteger(17, 4)).toEqual([4, 4, 4, 5]); }); test('should add zeros if value < numberOfParts', () => { expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]); + expect(splitInteger(3, 6)).toEqual([0, 0, 0, 1, 1, 1]); }); + +test('should always return an array of length numberOfParts', () => { + expect(splitInteger(5, 10)).toHaveLength(10); + expect(splitInteger(32, 6)).toHaveLength(6); +}); + +test('should always return an array if numberOfParts 0', () => { + expect(splitInteger(5, 0)).toBeInstanceOf(Array); +}); + +test('should return an array with integer in every case', () => { + expect(splitInteger(5, 3).every(Number.isInteger)).toBe(true); + expect(splitInteger(5, 10).every(Number.isInteger)).toBe(true); + expect(splitInteger(5, 0).every(Number.isInteger)).toBe(true); +}); + +test('should sum of parts always equal to value', () => { + const value = 17; + + expect(splitInteger(value, 3).reduce((a, b) => a + b, 0)).toBe(value); + expect(splitInteger(value, 10).reduce((a, b) => a + b, 0)).toBe(value); + expect(splitInteger(value, 1).reduce((a, b) => a + b, 0)).toBe(value); +}); + +test('should deference between max and min part to be <= 1', () => { + if (splitInteger().length > 0) { + expect(Math.max(splitInteger(17, 3)) - Math.min(splitInteger(17, 3))) + .toBeLessThanOrEqual(1); + + expect(Math.max(splitInteger(17, 10)) - Math.min(splitInteger(17, 10))) + .toBeLessThanOrEqual(1); + + expect(Math.max(splitInteger(17, 4)) - Math.min(splitInteger(17, 1))) + .toBeLessThanOrEqual(1); + + expect(Math.max(splitInteger(17, 0)) - Math.min(splitInteger(17, 0))) + .toBeLessThanOrEqual(1); + } +}); + +test('should return an array is sorted in non-decreasing order', () => { + expect(splitInteger(32, 6)).toEqual(splitInteger(32, 6).slice() + .sort((a, b) => a - b)); + + expect(splitInteger(17, 4)).toEqual(splitInteger(17, 4).slice() + .sort((a, b) => a - b)); + + expect(splitInteger(8, 1)).toEqual(splitInteger(8, 1).slice() + .sort((a, b) => a - b)); +}); + +test('should return an array with amount of larger parts to equal value % numberOfParts if not evenly divisible ', () => { + expect(splitInteger(17, 4).filter((part) => part === 5)).toHaveLength(1); + expect(splitInteger(32, 6).filter((part) => part === 6)).toHaveLength(2); +}); + + From 2ea5891f04031f56fe049bf5c25b749a6a08d53b Mon Sep 17 00:00:00 2001 From: Serhii-Khobotov Date: Thu, 11 Sep 2025 12:48:28 +0300 Subject: [PATCH 3/3] removed unnecessary cases --- src/splitInteger.test.js | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index bcbb713b..d9bfeb14 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -17,24 +17,13 @@ test('should sort parts ascending if they are not equal', () => { expect(splitInteger(17, 4)).toEqual([4, 4, 4, 5]); }); -test('should add zeros if value < numberOfParts', () => { - expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]); - expect(splitInteger(3, 6)).toEqual([0, 0, 0, 1, 1, 1]); -}); - test('should always return an array of length numberOfParts', () => { - expect(splitInteger(5, 10)).toHaveLength(10); expect(splitInteger(32, 6)).toHaveLength(6); }); -test('should always return an array if numberOfParts 0', () => { - expect(splitInteger(5, 0)).toBeInstanceOf(Array); -}); - test('should return an array with integer in every case', () => { expect(splitInteger(5, 3).every(Number.isInteger)).toBe(true); expect(splitInteger(5, 10).every(Number.isInteger)).toBe(true); - expect(splitInteger(5, 0).every(Number.isInteger)).toBe(true); }); test('should sum of parts always equal to value', () => { @@ -47,16 +36,13 @@ test('should sum of parts always equal to value', () => { test('should deference between max and min part to be <= 1', () => { if (splitInteger().length > 0) { - expect(Math.max(splitInteger(17, 3)) - Math.min(splitInteger(17, 3))) + expect(Math.max(...splitInteger(17, 3)) - Math.min(...splitInteger(17, 3))) .toBeLessThanOrEqual(1); - expect(Math.max(splitInteger(17, 10)) - Math.min(splitInteger(17, 10))) + expect(Math.max(...splitInteger(17, 10)) - Math.min(...splitInteger(17, 10))) .toBeLessThanOrEqual(1); - expect(Math.max(splitInteger(17, 4)) - Math.min(splitInteger(17, 1))) - .toBeLessThanOrEqual(1); - - expect(Math.max(splitInteger(17, 0)) - Math.min(splitInteger(17, 0))) + expect(Math.max(...splitInteger(17, 4)) - Math.min(...splitInteger(17, 4))) .toBeLessThanOrEqual(1); } }); @@ -76,5 +62,3 @@ test('should return an array with amount of larger parts to equal value % number expect(splitInteger(17, 4).filter((part) => part === 5)).toHaveLength(1); expect(splitInteger(32, 6).filter((part) => part === 6)).toHaveLength(2); }); - -