From c7d77cf2f507b84fb90a14a655aca6ec79a4afcf Mon Sep 17 00:00:00 2001 From: Vital Date: Fri, 3 Apr 2026 08:31:35 +0300 Subject: [PATCH 1/2] add solution --- src/reduce.test.js | 57 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index c3331e7..b48c60d 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -2,16 +2,59 @@ const { reduce } = require('./reduce'); -describe('reduce', () => { - beforeAll(() => { - Array.prototype.reduce2 = reduce; // eslint-disable-line +describe('Custom reduce method', () => { + let numbers; + + beforeEach(() => { + numbers = [1, 2, 3, 4]; + }); + + it('should sum all numbers with initial value', () => { + const result = reduce.call(numbers, (acc, num) => acc + num, 0); + + expect(result).toBe(10); }); - afterAll(() => { - delete Array.prototype.reduce2; + it('should sum all numbers without initial value', () => { + const result = reduce.call(numbers, (acc, num) => acc + num); + + expect(result).toBe(10); }); - it('should ', () => {}); + it('should multiply all numbers', () => { + const result = reduce.call(numbers, (acc, num) => acc * num, 1); + + expect(result).toBe(24); + }); + + it('should concatenate strings', () => { + const words = ['Hello', ' ', 'World']; + + const result = reduce.call(words, (acc, word) => acc + word, ''); + + expect(result).toBe('Hello World'); + }); - // Add tests here + it('should return initial value for empty array', () => { + const result = reduce.call([], (acc, num) => acc + num, 100); + + expect(result).toBe(100); + }); + + it('should call callback correct number of times', () => { + const callback = jest.fn((acc, num) => acc + num); + + reduce.call(numbers, callback, 0); + + expect(callback).toHaveBeenCalledTimes(4); + }); + + it('should pass correct arguments to callback', () => { + const callback = jest.fn((acc, num) => acc + num); + + reduce.call(numbers, callback, 0); + + expect(callback).toHaveBeenNthCalledWith(1, 0, 1, 0, numbers); + expect(callback).toHaveBeenNthCalledWith(2, 1, 2, 1, numbers); + }); }); From 8c45fd1b5544ab8a394671754999127b59e541ba Mon Sep 17 00:00:00 2001 From: Vital Date: Fri, 3 Apr 2026 08:42:11 +0300 Subject: [PATCH 2/2] add fixed --- src/reduce.js | 4 ++++ src/reduce.test.js | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/reduce.js b/src/reduce.js index 38be21c..3bd3b9b 100644 --- a/src/reduce.js +++ b/src/reduce.js @@ -7,6 +7,10 @@ * @returns {*} */ function reduce(callback, startValue) { + if (this.length === 0 && arguments.length < 2) { + throw new TypeError('Reduce of empty array with no initial value'); + } + let prev = startValue; let startIndex = 0; diff --git a/src/reduce.test.js b/src/reduce.test.js index b48c60d..c232ca2 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -41,6 +41,12 @@ describe('Custom reduce method', () => { expect(result).toBe(100); }); + it('should throw TypeError for empty array without initial value', () => { + expect(() => { + reduce.call([], (acc, num) => acc + num); + }).toThrow(TypeError); + }); + it('should call callback correct number of times', () => { const callback = jest.fn((acc, num) => acc + num);