-
Notifications
You must be signed in to change notification settings - Fork 113
Solution #78
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: master
Are you sure you want to change the base?
Solution #78
Changes from all commits
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 |
|---|---|---|
|
|
@@ -11,9 +11,66 @@ describe('reduce', () => { | |
| delete Array.prototype.reduce2; | ||
| }); | ||
|
|
||
| it('should ', () => { | ||
| it('should sum all numbers in array with initial value', () => { | ||
| const result = [1, 2, 3, 4].reduce2((acc, curr) => acc + curr, 0); | ||
|
|
||
| expect(result).toBe(10); | ||
| }); | ||
|
|
||
| // Add tests here | ||
| it('should sum all numbers in array without initial value', () => { | ||
| const result = [1, 2, 3, 4].reduce2((acc, curr) => acc + curr); | ||
|
|
||
| expect(result).toBe(10); | ||
| }); | ||
|
|
||
| it('should handle reducing to a single string', () => { | ||
| const result = ['a', 'b', 'c'].reduce2((acc, curr) => acc + curr, ''); | ||
|
|
||
| expect(result).toBe('abc'); | ||
| }); | ||
|
|
||
| it('should handle reducing an array of objects', () => { | ||
| const arr = [{ value: 1 }, { value: 2 }, { value: 3 }]; | ||
| const result = arr.reduce2((acc, curr) => acc + curr.value, 0); | ||
|
|
||
| expect(result).toBe(6); | ||
| }); | ||
|
|
||
| it('should return the first element if no initial value', | ||
| () => { | ||
| const result = [42].reduce2((acc, curr) => acc + curr); | ||
|
|
||
| expect(result).toBe(42); | ||
| }); | ||
|
|
||
| it('should return undefined if array is empty and no value is provided', | ||
| () => { | ||
| const result = [].reduce2((acc, curr) => acc + curr); | ||
|
|
||
| expect(result).toBe(undefined); | ||
| }); | ||
|
Comment on lines
+46
to
+51
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 |
||
|
|
||
| it('should pass index and array to callback', () => { | ||
| const mockFn = jest.fn((acc, curr, index, arr) => acc + curr); | ||
|
|
||
| [1, 2, 3].reduce2(mockFn, 0); | ||
|
|
||
| expect(mockFn).toHaveBeenCalledWith(0, 1, 0, [1, 2, 3]); | ||
| expect(mockFn).toHaveBeenCalledWith(1, 2, 1, [1, 2, 3]); | ||
| expect(mockFn).toHaveBeenCalledWith(3, 3, 2, [1, 2, 3]); | ||
| }); | ||
|
|
||
| it('should work correctly with holes in the array', () => { | ||
| const arr = [1, 3]; | ||
| const result = arr.reduce2((acc, curr) => acc + (curr || 0), 0); | ||
|
|
||
| expect(result).toBe(4); | ||
| }); | ||
|
|
||
| it('should allow reducing with boolean values', () => { | ||
| const result = [true, false, true].reduce2( | ||
| (acc, curr) => acc && curr, true); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
| }); | ||
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: According to the standard Array.prototype.reduce behavior, calling reduce on an empty array without an initial value should throw a TypeError, not return undefined. If your implementation is meant to match the standard, this test should expect an error to be thrown instead of returning undefined. Please review the requirements and adjust the test accordingly.