Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/customs.js
Original file line number Diff line number Diff line change
@@ -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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of the logic is flawed because it unconditionally returns true when a number is evenly divisible, without actually checking if the array is sorted. It's great that you're no longer using this function, and removing it entirely would be the best solution.

}

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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper makes the test that uses it more complex than necessary. A direct assertion like expect(result).toEqual([8]) in the test file is much clearer. By updating that test, you can remove this helper and eventually this entire file.

}

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,
};
21 changes: 18 additions & 3 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
@@ -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]);
Comment on lines +20 to +23

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job simplifying the other tests to use toEqual! For consistency, you can simplify this test as well. A single line expect(splitInteger(8, 1)).toEqual([8]); would be clearer and achieve the same goal without needing the custom helper.

});

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]);
});