-
Notifications
You must be signed in to change notification settings - Fork 113
Solution #86
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 #86
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 |
|---|---|---|
|
|
@@ -10,6 +10,10 @@ function reduce(callback, startValue) { | |
| let prev = startValue; | ||
| let startIndex = 0; | ||
|
|
||
| if (this.length === 0 && arguments.length < 2) { | ||
| throw new TypeError('Reduce of empty array with no initial value'); | ||
| } | ||
|
|
||
| if (arguments.length < 2) { | ||
| startIndex = 1; | ||
| prev = this[0]; | ||
|
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. When no initial value is provided, using |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,9 +11,73 @@ describe('reduce', () => { | |
| delete Array.prototype.reduce2; | ||
| }); | ||
|
|
||
| it('should ', () => { | ||
| it('should sum numbers with initial value', () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = arr.reduce2((acc, curr) => acc + curr, 10); | ||
|
|
||
| expect(result).toBe(16); | ||
| }); | ||
|
|
||
| // Add tests here | ||
| it('should sum numbers without initial value', () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = arr.reduce2((acc, curr) => acc + curr); | ||
|
|
||
| expect(result).toBe(6); | ||
| }); | ||
|
|
||
| it('should handle a single-element array without initial value', () => { | ||
| const arr = [5]; | ||
| const result = arr.reduce2((acc, curr) => acc + curr); | ||
|
|
||
| expect(result).toBe(5); | ||
| }); | ||
|
|
||
| it('should work with an initial value different from number', () => { | ||
| const arr = ['a', 'b', 'c']; | ||
| const result = arr.reduce2((acc, curr) => acc + curr, ''); | ||
|
|
||
| expect(result).toBe('abc'); | ||
| }); | ||
|
|
||
| it('should pass index and array as arguments to callback', () => { | ||
| const arr = [10, 20, 30]; | ||
| const mockCallback = jest.fn((acc, curr) => acc + curr); | ||
|
|
||
| arr.reduce2(mockCallback); | ||
|
|
||
| expect(mockCallback.mock.calls.length).toBe(2); | ||
| expect(mockCallback).toHaveBeenCalledWith(10, 20, 1, arr); | ||
| expect(mockCallback).toHaveBeenCalledWith(30, 30, 2, arr); | ||
| }); | ||
|
|
||
| it('should throw TypeError when called on empty array without initial value', | ||
| () => { | ||
| const arr = []; | ||
|
|
||
| expect(() => arr.reduce2((acc, curr) => acc + curr)).toThrow(TypeError); | ||
| }); | ||
|
|
||
| it('should return initial value when array is empty', () => { | ||
| const arr = []; | ||
| const result = arr.reduce2((acc, curr) => acc + curr, 0); | ||
|
|
||
| expect(result).toBe(0); | ||
| }); | ||
|
|
||
| it('should throw TypeError if callback is not a function', () => { | ||
| const arr = [1, 2, 3]; | ||
|
|
||
| expect(() => arr.reduce2(null)).toThrow(TypeError); | ||
| expect(() => arr.reduce2(123)).toThrow(TypeError); | ||
| expect(() => arr.reduce2({})).toThrow(TypeError); | ||
| expect(() => arr.reduce2('callback')).toThrow(TypeError); | ||
| }); | ||
|
|
||
| it('should skip empty slots in sparse arrays', () => { | ||
| const sparseArr = [1, , 3]; // eslint-disable-line no-sparse-arrays | ||
| const result = sparseArr.reduce2( | ||
| (acc, curr) => acc + (curr !== undefined && curr !== null ? curr : 0), 0); | ||
|
|
||
| expect(result).toBe(4); | ||
| }); | ||
|
Comment on lines
+76
to
+82
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. This test's name, 'should skip empty slots in sparse arrays', is misleading. The test implementation doesn't actually verify that the callback is skipped for empty slots. It only checks the final computed value, and the callback used ( |
||
| }); | ||
|
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. It's a good practice to test how the function handles sparse arrays (arrays with empty slots). The native it('should skip empty slots in sparse arrays', () => {
const sparseArr = [1, , 3]; // eslint-disable-line no-sparse-arrays
const result = sparseArr.reduce2((acc, curr) => acc + curr, 0);
expect(result).toBe(4);
});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. The test for callback arguments only covers the scenario where an initial value is provided. It would be beneficial to add another test case to verify that the correct arguments ( it('should pass correct arguments to callback without initial value', () => {
const arr = [10, 20, 30];
const mockCallback = jest.fn((acc, curr) => acc + curr);
arr.reduce2(mockCallback);
expect(mockCallback.mock.calls.length).toBe(2);
expect(mockCallback).toHaveBeenCalledWith(10, 20, 1, arr);
expect(mockCallback).toHaveBeenCalledWith(30, 30, 2, arr);
}); |
||
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.
The provided
callbackshould be validated to ensure it's a function. The nativeArray.prototype.reducethrows aTypeErrorif the callback argument is not a function. Please add this check at the beginning of your function.