Skip to content
Open
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
24 changes: 24 additions & 0 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,44 @@

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Add an explicit check that the result is an array before using array methods. The task requires the function to return an array; currently you call result.every(...) which will throw if result is not an array. Add expect(Array.isArray(result)).toBe(true); before the every assertion.

expect(result.every(Number.isInteger)).toBe(true);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

result.every(Number.isInteger) is called before asserting the return type. If result is not an array this call will throw and the test won't give a clear failure about return type. Move or add the Array.isArray assertion so it runs before this line.


expect(result).toHaveLength(numberOfParts);

for (let i = 0; i < result.length - 1; i++) {
expect(result[i]).toBeLessThanOrEqual(result[i + 1]);
}
expect(Math.max(...result) - Math.min(...result)).toBeLessThanOrEqual(1);
expect(result.reduce((a, b) => a + b, 0)).toBe(value);

return result;
}

test(`should split a number into equal parts
if a value is divisible by a numberOfParts`, () => {
const result = assertProperties(6, 2);

expect(result).toEqual([3, 3]);
});

test(`should return a part equals to a value
when splitting into 1 part`, () => {
const result = assertProperties(8, 1);

expect(result).toEqual([8]);
});

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

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

test('should add zeros if value < numberOfParts', () => {
const result = assertProperties(2, 5);

expect(result).toEqual([0, 0, 0, 1, 1]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Two tests are still missing from the checklist:

  1. The example splitInteger(32, 6) === [5, 5, 5, 5, 6, 6] — add a test that calls const result = assertProperties(32, 6); expect(result).toEqual([5, 5, 5, 5, 6, 6]);.

  2. The edge case where value === numberOfParts (array of ones). e.g. const result = assertProperties(5, 5); expect(result).toEqual([1, 1, 1, 1, 1]);.

Add these tests (after the existing ones) so all provided examples and required edge cases are covered.

});