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
15 changes: 7 additions & 8 deletions src/splitInteger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
* @returns {number[]}
*/
function splitInteger(value, numberOfParts) {
const parts = [];
let rest = value;
const base = Math.floor(value / numberOfParts);
const remainder = value % numberOfParts;

for (let partsLeft = numberOfParts; partsLeft > 0; partsLeft--) {
const part = Math.floor(rest / partsLeft);
const parts = new Array(numberOfParts).fill(base);

parts.push(part);
rest -= part;
for (let i = numberOfParts - remainder; i < numberOfParts; i++) {
parts[i] += 1;
Comment on lines +15 to +16

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 loop currently distributes the remainder to the last remainder elements. To ensure the difference between the largest and smallest parts is minimized, the remainder should be distributed to the first remainder elements instead. Consider adjusting the loop to iterate from 0 to remainder - 1.

Comment on lines +15 to +16

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 remainder is currently being distributed to the last elements of the array (from index numberOfParts - remainder to numberOfParts - 1).

According to the requirements, the remainder should be distributed to the first elements (from index 0 to remainder - 1). This ensures the smallest possible difference between the largest and smallest parts and that the result is sorted in ascending order after distribution. Please update the loop to distribute the remainder to the first elements instead.

}

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

module.exports = splitInteger;
module.exports = splitInteger;
17 changes: 13 additions & 4 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ const splitInteger = require('./splitInteger');

test(`should split a number into equal parts
if a value is divisible by a numberOfParts`, () => {

expect(splitInteger(6, 3)).toEqual([2, 2, 2]);
expect(splitInteger(10, 5)).toEqual([2, 2, 2, 2, 2]);
});

test(`should return a part equals to a value
when splitting into 1 part`, () => {

expect(splitInteger(8, 1)).toEqual([8]);
});

test('should sort parts ascending if they are not equal', () => {

const result = splitInteger(17, 4);
expect(result).toEqual([4, 4, 4, 5]);
const sorted = [...result].sort((a, b) => a - b);
expect(result).toEqual(sorted);
});

test('should add zeros if value < numberOfParts', () => {

const result = splitInteger(3, 5);
expect(result).toHaveLength(5);
expect(result.reduce((a,b) => a+b, 0)).toBe(3);
expect(Math.max(...result) - Math.min(...result)).toBeLessThanOrEqual(1);
const sorted = [...result].sort((a, b) => a - b);
expect(result).toEqual(sorted);
});