diff --git a/src/customs.js b/src/customs.js new file mode 100644 index 00000000..39f725aa --- /dev/null +++ b/src/customs.js @@ -0,0 +1,43 @@ +'use strict'; + +function toBeSortedAscending(arr, number, parts) { + if (hasRemainder(number, parts) === false) { + return arr.every((val, i) => i === 0 || arr[i - 1] <= val); + } + + return true; +} + +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..29fec8c9 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -1,21 +1,36 @@ '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`, () => { - + expect(splitInteger(6, 2)).toEqual([3, 3]); }); test(`should return a part equals to a value when splitting into 1 part`, () => { + const result = splitInteger(8, 1); + const resultOfCustoms = hasEqualValueToNumber(result, 8); + expect(resultOfCustoms).toEqual([8]); }); test('should sort parts ascending if they are not equal', () => { - + // const result = splitInteger(17, 4); + // expect(toBeSortedAscending(result)).toBe(true); + expect(splitInteger(17, 4)).toEqual([4, 4, 4, 5]); }); test('should add zeros if value < numberOfParts', () => { - + // const result = howManyZerosInArray(splitInteger(2, 4)); + // expect(zerosCounts(2, 4)).toBe(result); + expect(splitInteger(2, 4)).toEqual([0, 0, 1, 1]); });