From dad3e759d87dc9cb773c0ce2a2c7e2e023ef9f0b Mon Sep 17 00:00:00 2001 From: Nazarii Paliichuk Date: Fri, 20 Jun 2025 16:21:08 +0300 Subject: [PATCH 1/2] Solution --- src/reduce.test.js | 70 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 47a892f..c59928e 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -11,9 +11,75 @@ 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); + }); + + it('should not call callback if array has one element and no initial value', + () => { + const callback = jest.fn(); + + [5].reduce2(callback); + + expect(callback).not.toHaveBeenCalled(); + }); + + 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); + }); }); From de6ddd6a2496d7af943bbad27edeed38ab2100bf Mon Sep 17 00:00:00 2001 From: Nazarii Paliichuk Date: Fri, 20 Jun 2025 16:37:38 +0300 Subject: [PATCH 2/2] Solution --- src/reduce.test.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index c59928e..e085290 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -50,15 +50,6 @@ describe('reduce', () => { expect(result).toBe(undefined); }); - it('should not call callback if array has one element and no initial value', - () => { - const callback = jest.fn(); - - [5].reduce2(callback); - - expect(callback).not.toHaveBeenCalled(); - }); - it('should pass index and array to callback', () => { const mockFn = jest.fn((acc, curr, index, arr) => acc + curr);