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
2 changes: 1 addition & 1 deletion src/splitInteger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
"use strict";

/**
* @param {number} value
Expand Down
41 changes: 36 additions & 5 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
'use strict';
"use strict";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The test suite is missing the required example cases splitInteger(17, 4) and splitInteger(32, 6) which the task description expects to be tested. Add tests that assert those exact behaviors (or the required properties for those inputs). The description lists the examples that must be satisfied: splitInteger(8,1), splitInteger(6,2), splitInteger(17,4), and splitInteger(32,6) .


const splitInteger = require('./splitInteger');
const splitInteger = require("./splitInteger");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider adding a small helper function in this test file (e.g., assertSplitProperties(value, numberOfParts)) that runs the common assertions: length equals numberOfParts, all elements are integers, array is sorted ascending, sum equals value, and max - min <= 1. Call it for the required examples and edge cases to avoid duplicated code and ensure consistency across tests.


function assertSplitProperties(value, numberOfParts) {
const result = splitInteger(value, numberOfParts);

expect(result).toHaveLength(numberOfParts);

expect(result.reduce((acc, val) => acc + val, 0)).toBe(value);

result.forEach((el) => expect(Number.isInteger(el)).toBe(true));

for (let i = 0; i < result.length - 1; i++) {
expect(result[i + 1]).toBeGreaterThanOrEqual(result[i]);
}

const min = Math.min(...result);
const max = Math.max(...result);

expect(max - min).toBeLessThanOrEqual(1);

return result;
}

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

expect(assertSplitProperties(6, 2)).toEqual([3, 3]);
});

test(`should return a part equals to a value
when splitting into 1 part`, () => {
expect(assertSplitProperties(8, 1)).toEqual([8]);
});

test("should sort parts ascending if they are not equal", () => {
const result = assertSplitProperties(17, 4);

expect(result).toEqual([4, 4, 4, 5]);
});

test('should sort parts ascending if they are not equal', () => {
test("should add zeros if value < numberOfParts", () => {
const result = assertSplitProperties(5, 6);

expect(result).toContain(0);
});

test('should add zeros if value < numberOfParts', () => {
test("should handle example splitInteger(32, 6)", () => {
const result = assertSplitProperties(32, 6);

expect(result).toEqual([5, 5, 5, 5, 6, 6]);
});