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: 2 additions & 0 deletions src/splitInteger.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ function splitInteger(value, numberOfParts) {
return parts;
}

// console.log(splitInteger(12, 0));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove the commented-out debugging line // console.log(splitInteger(12, 0));. It is unnecessary in production/test code and may confuse reviewers. The function already exports correctly, so the log can be deleted. (Line currently commented out.)


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove the extra blank/commentable marker line. Keeping stray blank/commentable lines is harmless but removing it keeps the file tidy.

module.exports = splitInteger;
49 changes: 46 additions & 3 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,61 @@

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

expect(splitInteger(12, 3)).toEqual([4, 4, 4]);
});

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', () => {
expect(splitInteger(8, 3)).toEqual([2, 3, 3]);
expect(splitInteger(17, 4)).toEqual([4, 4, 4, 5]);
});

test('should always return an array of length numberOfParts', () => {
expect(splitInteger(32, 6)).toHaveLength(6);
});

test('should return an array with integer in every case', () => {
expect(splitInteger(5, 3).every(Number.isInteger)).toBe(true);
expect(splitInteger(5, 10).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.

Suggestion: This assertion uses splitInteger(5, 10) which is a valid input (numberOfParts > value). Keep the test if you intend to support that case, but ensure the implementation returns a balanced distribution (zeros then ones) so every(Number.isInteger) and other expectations hold. If you intended to forbid numberOfParts > value, update the test instead. The task description allows any positive integers.

});

test('should sum of parts always equal to value', () => {
const value = 17;

expect(splitInteger(value, 3).reduce((a, b) => a + b, 0)).toBe(value);
expect(splitInteger(value, 10).reduce((a, b) => a + b, 0)).toBe(value);
expect(splitInteger(value, 1).reduce((a, b) => a + b, 0)).toBe(value);
});

test('should add zeros if value < numberOfParts', () => {
test('should deference between max and min part to be <= 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.

Minor: Typo in test title — "deference" should be "difference". Fixing this improves clarity of the test description but does not affect behavior.

if (splitInteger().length > 0) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Critical: This conditional uses a call with no arguments and causes the assertions inside to be skipped. Calling splitInteger() without parameters is unintended; remove the if guard and run the max/min assertions directly for concrete inputs (e.g., splitInteger(17, 3)). This test is intended to ensure the difference between max and min is <= 1 and must not be bypassed. See the task requirements about max-min <= 1.

expect(Math.max(...splitInteger(17, 3)) - Math.min(...splitInteger(17, 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.

Suggestion to make assertions more robust/clear: avoid calling splitInteger(...) multiple times inside the same assertion. Instead store the result in a local variable and reuse it. For example, replace repeated calls in these lines with const parts = splitInteger(17, 3); expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1);. This reduces duplicated calls and makes failures easier to debug.

.toBeLessThanOrEqual(1);

expect(Math.max(...splitInteger(17, 10)) - Math.min(...splitInteger(17, 10)))

Check failure on line 42 in src/splitInteger.test.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Line 42 exceeds the maximum line length of 80
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Similar suggestion for the other max/min assertions: store const parts = splitInteger(17, 10) and const parts2 = splitInteger(17, 4) before asserting on Math.max/Math.min. This keeps the tests deterministic and clearer.

.toBeLessThanOrEqual(1);

expect(Math.max(...splitInteger(17, 4)) - Math.min(...splitInteger(17, 4)))
.toBeLessThanOrEqual(1);
}
});

test('should return an array is sorted in non-decreasing order', () => {
expect(splitInteger(32, 6)).toEqual(splitInteger(32, 6).slice()
.sort((a, b) => a - b));

expect(splitInteger(17, 4)).toEqual(splitInteger(17, 4).slice()
.sort((a, b) => a - b));

expect(splitInteger(8, 1)).toEqual(splitInteger(8, 1).slice()
.sort((a, b) => a - b));
});

test('should return an array with amount of larger parts to equal value % numberOfParts if not evenly divisible ', () => {

Check failure on line 61 in src/splitInteger.test.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Line 61 exceeds the maximum line length of 80
expect(splitInteger(17, 4).filter((part) => part === 5)).toHaveLength(1);
expect(splitInteger(32, 6).filter((part) => part === 6)).toHaveLength(2);
});
Loading