From 2d3cd6fdb882018a8c59aad029878b3db21eb909 Mon Sep 17 00:00:00 2001 From: Anna Lashkevych Date: Tue, 27 May 2025 22:23:28 +0300 Subject: [PATCH 1/2] Solution --- src/splitInteger.test.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index a610317d..fc746a8c 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -19,3 +19,32 @@ test('should sort parts ascending if they are not equal', () => { test('should add zeros if value < numberOfParts', () => { }); + +test(`should split a number into equal parts + if value is divisible by numberOfParts`, () => { + const result = splitInteger(10, 5); + + expect(result).toEqual([2, 2, 2, 2, 2]); +}); + +test('should return a part equals to value when splitting into 1 part', () => { + const result = splitInteger(7, 1); + + expect(result).toEqual([7]); +}); + +test('should sort parts ascending if they are not equal', () => { + const result = splitInteger(10, 3); + + // 10 / 3 = 3.33..., so expected parts: [3, 3, 4] or similar + expect(result).toEqual([3, 3, 4]); + expect([...result].sort((a, b) => a - b)).toEqual(result); // Check ascending +}); + +test('should add zeros if value < numberOfParts', () => { + const result = splitInteger(2, 5); + + // 2 parts of 1, rest are zeros: [0, 0, 1, 1, 0] sorted = [0, 0, 0, 1, 1] + expect(result).toEqual([0, 0, 1, 1, 0].sort((a, b) => a - b)); + expect(result.reduce((a, b) => a + b, 0)).toBe(2); +}); From 5d221a43fca8b2f590c905b26e76dc5ff6c814d5 Mon Sep 17 00:00:00 2001 From: Anna Lashkevych Date: Tue, 27 May 2025 23:04:54 +0300 Subject: [PATCH 2/2] Solution --- src/splitInteger.test.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index fc746a8c..75f95a47 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -20,7 +20,7 @@ test('should add zeros if value < numberOfParts', () => { }); -test(`should split a number into equal parts +test(`should split a number into equal parts if value is divisible by numberOfParts`, () => { const result = splitInteger(10, 5); @@ -38,13 +38,15 @@ test('should sort parts ascending if they are not equal', () => { // 10 / 3 = 3.33..., so expected parts: [3, 3, 4] or similar expect(result).toEqual([3, 3, 4]); - expect([...result].sort((a, b) => a - b)).toEqual(result); // Check ascending + expect([...result].sort((a, b) => a - b)).toEqual(result); }); test('should add zeros if value < numberOfParts', () => { const result = splitInteger(2, 5); - // 2 parts of 1, rest are zeros: [0, 0, 1, 1, 0] sorted = [0, 0, 0, 1, 1] - expect(result).toEqual([0, 0, 1, 1, 0].sort((a, b) => a - b)); + // 2 parts of 1, rest are zeros — order doesn't matter + const expected = [0, 0, 0, 1, 1]; + + expect([...result].sort((a, b) => a - b)).toEqual(expected); expect(result.reduce((a, b) => a + b, 0)).toBe(2); });