From 1e5043f6d9f109630581331c06cb823311a5fea2 Mon Sep 17 00:00:00 2001 From: Kipkalo Date: Tue, 27 May 2025 10:26:01 +0300 Subject: [PATCH 1/3] test added --- src/splitInteger.test.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index a610317d..3f6ec9a0 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -4,18 +4,27 @@ const splitInteger = require('./splitInteger'); test(`should split a number into equal parts if a value is divisible by a numberOfParts`, () => { - + expect(splitInteger(6, 3)).toEqual([2, 2, 2]); + expect(splitInteger(10, 5)).toEqual([2, 2, 2, 2, 2]); }); test(`should return a part equals to a value when splitting into 1 part`, () => { - + expect(splitInteger(8, 1)).toEqual([8]); }); test('should sort parts ascending if they are not equal', () => { - + const result = splitInteger(17, 4); + expect(result).toEqual([4, 4, 4, 5]); + const sorted = [...result].sort((a, b) => a - b); + expect(result).toEqual(sorted); }); test('should add zeros if value < numberOfParts', () => { - + const result = splitInteger(3, 5); + expect(result).toHaveLength(5); + expect(result.reduce((a,b) => a+b, 0)).toBe(3); + expect(Math.max(...result) - Math.min(...result)).toBeLessThanOrEqual(1); + const sorted = [...result].sort((a, b) => a - b); + expect(result).toEqual(sorted); }); From 2000cbe05df9f8a9ae3445776a20245f64b1cb93 Mon Sep 17 00:00:00 2001 From: Kipkalo Date: Tue, 27 May 2025 10:56:47 +0300 Subject: [PATCH 2/3] Update --- src/splitInteger.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/splitInteger.js b/src/splitInteger.js index d3da7485..6a87d9c6 100644 --- a/src/splitInteger.js +++ b/src/splitInteger.js @@ -6,18 +6,19 @@ * * @returns {number[]} */ +'use strict'; + function splitInteger(value, numberOfParts) { - const parts = []; - let rest = value; + const base = Math.floor(value / numberOfParts); + const remainder = value % numberOfParts; - for (let partsLeft = numberOfParts; partsLeft > 0; partsLeft--) { - const part = Math.floor(rest / partsLeft); + const parts = new Array(numberOfParts).fill(base); - parts.push(part); - rest -= part; + for (let i = numberOfParts - remainder; i < numberOfParts; i++) { + parts[i] += 1; } return parts; } -module.exports = splitInteger; +module.exports = splitInteger; \ No newline at end of file From 74e34adcb4a97c42ee5e2314387d52b7083ceccf Mon Sep 17 00:00:00 2001 From: Kipkalo Date: Tue, 27 May 2025 11:51:20 +0300 Subject: [PATCH 3/3] updadted/reworked tests --- src/splitInteger.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/splitInteger.js b/src/splitInteger.js index 6a87d9c6..ae9a1b58 100644 --- a/src/splitInteger.js +++ b/src/splitInteger.js @@ -6,8 +6,6 @@ * * @returns {number[]} */ -'use strict'; - function splitInteger(value, numberOfParts) { const base = Math.floor(value / numberOfParts); const remainder = value % numberOfParts; @@ -18,7 +16,7 @@ function splitInteger(value, numberOfParts) { parts[i] += 1; } - return parts; + return parts.sort((a, b) => a - b); } module.exports = splitInteger; \ No newline at end of file