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
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"eslint": "^5.16.0",
"eslint-plugin-jest": "^22.4.1",
"eslint-plugin-node": "^8.0.1",
"jest": "^24.5.0"
"jest": "^24.9.0"
},
"mateAcademy": {
"projectType": "javascript"
Expand Down
54 changes: 50 additions & 4 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,65 @@
const splitInteger = require('./splitInteger');

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

if a value is divisible by numberOfParts`, () => {
expect(splitInteger(6, 3)).toEqual([2, 2, 2]);
expect(splitInteger(12, 4)).toEqual([3, 3, 3, 3]);
});

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

expect(splitInteger(8, 1)).toEqual([8]);
expect(splitInteger(123, 1)).toEqual([123]);
});

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

expect(result).toEqual([4, 4, 4, 5]);
expect(result).toEqual([...result].sort((a, b) => a - b));
Comment on lines +20 to +21

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The test expects the result to be [4, 4, 4, 5] and also checks that the result is sorted in ascending order. If the implementation returns [5, 4, 4, 4], the test will fail, even though the sum and max-min difference conditions are satisfied. Please clarify whether the output must always be sorted, and ensure the expected output matches this requirement.

});

test('should add zeros if value < numberOfParts', () => {
expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]);
expect(splitInteger(0, 3)).toEqual([0, 0, 0]);
expect(splitInteger(1, 4)).toEqual([0, 0, 0, 1]);
});

test('should ensure the sum of parts equals the original value', () => {
const testCases = [
[8, 1],
[6, 2],
[17, 4],
[32, 6],
[0, 3],
[3, 5],
[1, 4],
[100, 9],
];

for (const [value, parts] of testCases) {
const result = splitInteger(value, parts);
const sum = result.reduce((a, b) => a + b, 0);

expect(sum).toBe(value);
}
});

test('should ensure the difference between max and min is at most 1', () => {
const testCases = [
[17, 4],
[32, 6],
[3, 5],
[0, 3],
[1, 4],
[100, 9],
];

for (const [value, parts] of testCases) {
const result = splitInteger(value, parts);
const min = Math.min(...result);
const max = Math.max(...result);

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