-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add tests for splitInteger function #910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
dde3abf
2189647
8d59c03
d240819
6702787
95d30d3
5f573be
863f2a2
3c0f1b6
487d1b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| function splitInteger(value, numberOfParts) { | ||
| const base = Math.floor(value / numberOfParts); | ||
| const remainder = value % numberOfParts; | ||
| const result = Array(numberOfParts).fill(base); | ||
| for (let i = 0; i < remainder; i++) { | ||
| result[numberOfParts - 1 - i]++; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: The task requires that the largest values should be at the beginning of the result array. However, you are incrementing the last elements ( |
||
| } | ||
| return result.sort((a, b) => a - b); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: Sorting the result array at the end is unnecessary and incorrect for this task. The task requires the larger values to be at the beginning, not sorted in ascending order. Remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: The |
||
| } | ||
|
|
||
| module.exports = splitInteger; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,20 +2,68 @@ | |
|
|
||
| const splitInteger = require('./splitInteger'); | ||
|
|
||
| test(`should split a number into equal parts | ||
| if a value is divisible by a numberOfParts`, () => { | ||
| test( | ||
| 'should split a number into equal parts if 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 equal to value when splitting into 1 part', | ||
| () => { | ||
| expect(splitInteger(8, 1)).toEqual([8]); | ||
| expect(splitInteger(123, 1)).toEqual([123]); | ||
| } | ||
| ); | ||
|
|
||
| test(`should return a part equals to a value | ||
| when splitting into 1 part`, () => { | ||
| test('should sort parts ascending if they are not equal', () => { | ||
| const result = splitInteger(17, 4); | ||
| expect(result).toEqual([4, 4, 4, 5]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: The expected array |
||
| expect(result).toEqual([...result].sort((a, b) => a - b)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: The task requires that the largest values should be at the beginning of the result array, not sorted in ascending order. This test enforces ascending order, which is contrary to the requirements. Please remove or update this test to match the task description. |
||
| }); | ||
|
Comment on lines
+22
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: The test expects the result to be sorted in ascending order and explicitly checks this with |
||
|
|
||
| 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]); | ||
|
Comment on lines
+29
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: The expected arrays in these tests are in ascending order (e.g., |
||
| }); | ||
|
|
||
| test('should sort parts ascending if they are not equal', () => { | ||
| 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 add zeros if value < numberOfParts', () => { | ||
| 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); | ||
| } | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue: You are incrementing the last elements of the result array, then sorting at the end. According to the task, the largest values should be at the beginning of the array, not the end. Instead of incrementing from the end, increment from the start (i.e., use
result[i]++in the loop). This will ensure the larger values are at the beginning, as required by the task description.