From 46c1598540f15552eae99463afaa8e5f8548cca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=C5=81abucki?= Date: Fri, 16 Jan 2026 20:06:04 +0100 Subject: [PATCH] Solution --- src/customs.js | 41 ++++++++++++++++++++++++++++++++++++++++ src/splitInteger.test.js | 18 ++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/customs.js diff --git a/src/customs.js b/src/customs.js new file mode 100644 index 00000000..1cf6df53 --- /dev/null +++ b/src/customs.js @@ -0,0 +1,41 @@ +'use strict'; + +function toBeSortedAscending(arr) { + if (hasRemainder(arr) === false) { + return arr.every((val, i) => i === 0 || arr[i - 1] <= val); + } +} + +function hasRemainder(number, parts) { + return number % parts === 0; +} + +function hasSpilitEqual(result, number, parts) { + const remainder = hasRemainder(number, parts); + const length = result.length === parts; + const equality + = result[0] === result[result.length - 1] + && result.every((e) => e === result[0]); + + return remainder ? (length ? equality : false) : false; +} + +function hasEqualValueToNumber(result, number) { + return result[0] === number ? result : false; +} + +function zerosCounts(number, parts) { + return Math.max(0, parts - number); +} + +function howManyZerosInArray(result) { + return result.reduce((acc, val) => (val === 0 ? acc + 1 : acc), 0); +} + +module.exports = { + toBeSortedAscending, + hasSpilitEqual, + hasEqualValueToNumber, + zerosCounts, + howManyZerosInArray, +}; diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index a610317d..6eadcfa2 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -1,21 +1,39 @@ 'use strict'; +const { + toBeSortedAscending, + hasSpilitEqual, + hasEqualValueToNumber, + zerosCounts, + howManyZerosInArray, +} = require('./customs'); + const splitInteger = require('./splitInteger'); test(`should split a number into equal parts if a value is divisible by a numberOfParts`, () => { + const result = splitInteger(34, 2); + const resultOfCustoms = hasSpilitEqual(result, 34, 2); + expect(resultOfCustoms).toEqual(true); }); test(`should return a part equals to a value when splitting into 1 part`, () => { + const result = splitInteger(34, 1); + const resultOfCustoms = hasEqualValueToNumber(result, 34); + expect(resultOfCustoms).toEqual([34]); }); test('should sort parts ascending if they are not equal', () => { + const result = splitInteger(34, 5); + expect(toBeSortedAscending(result)).toBe(true); }); test('should add zeros if value < numberOfParts', () => { + const result = howManyZerosInArray(splitInteger(2, 4)); + expect(zerosCounts(2, 4)).toBe(result); });