Skip to content
Open
Changes from 2 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
14 changes: 6 additions & 8 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ const splitInteger = require('./splitInteger');

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

});

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

expect(splitInteger(17, 4))
.toHaveLength(4);
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 calls splitInteger(17, 4) but the test title says "if a value is divisible by a numberOfParts" — 17 is not divisible by 4. Also this test only asserts the returned array length. Per the task requirements you must assert the actual distribution (for (17,4) the expected array is [4,4,4,5]) or use a truly divisible example such as splitInteger(8, 1) or splitInteger(6, 2). Additionally, each test should check the other required properties: sum equals the input value, all elements are integers, array is sorted non-decreasingly, and max - min <= 1.

Consider changing this assertion to one of the following approaches:

  • If you want to keep the example (17, 4):
const parts = splitInteger(17, 4);
expect(parts).toEqual([4, 4, 4, 5]);
expect(parts).toHaveLength(4);
expect(parts.reduce((a, b) => a + b, 0)).toBe(17);
expect(parts.every(Number.isInteger)).toBe(true);
expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1);
  • Or change the input to a divisible case like splitInteger(6, 2) and assert toEqual([3, 3]) plus the other properties.

This is necessary to satisfy these checklist items: exact example outputs, sum preservation, integer elements, sortedness, and max-min difference requirement.

});

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

expect(splitInteger(32, 6))
.toEqual([5, 5, 5, 5, 6, 6]);
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 assertion toEqual([5, 5, 5, 5, 6, 6]) correctly matches the provided example for splitInteger(32, 6) and should be kept. However, the task requires tests to also verify the general properties for returned arrays (length correctness, integer elements, sorted non-decreasing order, max - min <= 1, and sum equals the original value). Relying on a single toEqual check covers content but you should add explicit assertions for those properties (or add separate tests that assert them) so the implementation is fully validated against the checklist.

Example additional assertions:

const parts = splitInteger(32, 6);
expect(parts).toHaveLength(6);
expect(parts.every(Number.isInteger)).toBe(true);
expect(parts.reduce((a,b)=>a+b,0)).toBe(32);
expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1);

Adding these ensures the test suite verifies all required behaviors, not just this single example match.

});

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

expect(splitInteger(17, 0))
.toEqual([]);
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 test is invalid given the task's input assumptions and the test title. It calls splitInteger(17, 0) (zero parts) but the description states inputs are positive integers and the test title says "should add zeros if value < numberOfParts". numberOfParts should not be 0. Replace this test with a proper "value < numberOfParts" case, for example splitInteger(2, 5) which should return [0,0,0,1,1] (sorted) and satisfy max - min <= 1 and sum equality. Also add assertions for length and integer elements.

Suggested replacement:

const parts = splitInteger(2, 5);
expect(parts).toEqual([0, 0, 0, 1, 1]);
expect(parts).toHaveLength(5);
expect(parts.reduce((a,b)=>a+b,0)).toBe(2);
expect(parts.every(Number.isInteger)).toBe(true);
expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1);

This aligns the test input with the task requirements and the test title.

});