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
8 changes: 8 additions & 0 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ const splitInteger = require('./splitInteger');

test(`should split a number into equal parts
if a value is divisible by a numberOfParts`, () => {
const result = splitInteger(6, 2);
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 computes result for splitInteger(6, 2) — good. Consider adding explicit property assertions here (instead of relying only on exact array equality) to verify the checklist items: array type, correct length, integer elements, sum equals input, sortedness, and max-min <= 1. For example:

expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(2);
expect(result.every(Number.isInteger)).toBe(true);
expect(result.reduce((a,b)=>a+b,0)).toBe(6);
expect(Math.max(...result) - Math.min(...result)).toBeLessThanOrEqual(1);
expect(result).toEqual([...result].sort((a,b)=>a-b));

Adding these checks will ensure the test verifies the general properties from the task, not just one concrete output. (See checklist items 3.1–3.5.)


expect(result).toEqual([3, 3]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This expect(result).toEqual([3, 3]) is fine for the specific example, but it would be more robust to also assert the general properties (length, sum, integer-ness, sorted, max-min <= 1). That way tests document the required behavior rather than only one canonical output.

});

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

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

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.

Good that you check a non-equal-parts example here. Also consider adding the additional example from the description splitInteger(32, 6) === [5, 5, 5, 5, 6, 6] to mirror the specification. More importantly, add a general-property assertion (array type, length, integers, sum, sorted, max-min <=1) either here or in a separate helper test so the checklist items are explicitly verified.

});

test('should add zeros if value < numberOfParts', () => {
const result = splitInteger(3, 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.

The edge-case test for value < numberOfParts is present and asserts the expected array. Please also assert result.length === 5, that all elements are integers, the sum equals the original value (3), that the array is sorted, and Math.max(...result) - Math.min(...result) <= 1. These explicit checks ensure the function adheres to the task constraints for such edge cases (checklist items 3.1, 3.2, 3.3, 3.4, 3.5).


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