Skip to content
11 changes: 11 additions & 0 deletions splitInteger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function splitInteger(value, numberOfParts) {
const base = Math.floor(value / numberOfParts);
const remainder = value % numberOfParts;
const result = Array(numberOfParts).fill(base);
for (let i = 0; i < remainder; i++) {
result[numberOfParts - 1 - i]++;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: You are incrementing the last elements of the result array, then sorting at the end. According to the task, the largest values should be at the beginning of the array, not the end. Instead of incrementing from the end, increment from the start (i.e., use result[i]++ in the loop). This will ensure the larger values are at the beginning, as required by the task description.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The task requires that the largest values should be at the beginning of the result array. However, you are incrementing the last elements (result[numberOfParts - 1 - i]++). You should increment the first elements (result[i]++) to ensure the largest values are at the start.

}
return result.sort((a, b) => a - b);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: Sorting the result array at the end is unnecessary and incorrect for this task. The task requires the larger values to be at the beginning, not sorted in ascending order. Remove the .sort((a, b) => a - b) and ensure the array is constructed in the correct order directly.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The .sort((a, b) => a - b) call places the smallest values at the beginning, which is contrary to the requirement that the largest values should be at the start. You should remove the sort and return the array as-is after incrementing the first elements.

}

module.exports = splitInteger;
62 changes: 55 additions & 7 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,68 @@

const splitInteger = require('./splitInteger');

test(`should split a number into equal parts
if a value is divisible by a numberOfParts`, () => {
test(
'should split a number into equal parts if value is divisible by ' +
'numberOfParts',
() => {
expect(splitInteger(6, 3)).toEqual([2, 2, 2]);
expect(splitInteger(12, 4)).toEqual([3, 3, 3, 3]);
}
);

});
test(
'should return a part equal to value when splitting into 1 part',
() => {
expect(splitInteger(8, 1)).toEqual([8]);
expect(splitInteger(123, 1)).toEqual([123]);
}
);

test(`should return a part equals to a value
when splitting into 1 part`, () => {
test('should sort parts ascending if they are not equal', () => {
const result = splitInteger(17, 4);
expect(result).toEqual([4, 4, 4, 5]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The expected array [4, 4, 4, 5] is in ascending order, but the task requires the largest values at the start. The expected result should be [5, 4, 4, 4].

expect(result).toEqual([...result].sort((a, b) => a - b));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The task requires that the largest values should be at the beginning of the result array, not sorted in ascending order. This test enforces ascending order, which is contrary to the requirements. Please remove or update this test to match the task description.

});
Comment on lines +22 to +26

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The test expects the result to be sorted in ascending order and explicitly checks this with expect(result).toEqual([...result].sort((a, b) => a - b));. According to the task requirements, the largest values should be at the beginning of the result array, so this test should be updated or removed.


test('should add zeros if value < numberOfParts', () => {
expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]);
expect(splitInteger(0, 3)).toEqual([0, 0, 0]);
expect(splitInteger(1, 4)).toEqual([0, 0, 0, 1]);
Comment on lines +29 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The expected arrays in these tests are in ascending order (e.g., [0, 0, 1, 1, 1], [0, 0, 0, 1]), but the task requires the largest values at the start. Please update the expected arrays so that the largest values are at the beginning (e.g., [1, 1, 1, 0, 0], [1, 0, 0, 0]).

});

test('should sort parts ascending if they are not equal', () => {
test('should ensure the sum of parts equals the original value', () => {
const testCases = [
[8, 1],
[6, 2],
[17, 4],
[32, 6],
[0, 3],
[3, 5],
[1, 4],
[100, 9],
];

for (const [value, parts] of testCases) {
const result = splitInteger(value, parts);
const sum = result.reduce((a, b) => a + b, 0);
expect(sum).toBe(value);
}
});

test('should add zeros if value < numberOfParts', () => {
test('should ensure the difference between max and min is at most 1', () => {
const testCases = [
[17, 4],
[32, 6],
[3, 5],
[0, 3],
[1, 4],
[100, 9],
];

for (const [value, parts] of testCases) {
const result = splitInteger(value, parts);
const min = Math.min(...result);
const max = Math.max(...result);
expect(max - min).toBeLessThanOrEqual(1);
}
});