From f28dad17e30b4f71a0e1f9fb14fac0de322f8a3e Mon Sep 17 00:00:00 2001 From: Viktoriia Pylypko Date: Sun, 20 Jul 2025 15:01:14 +0300 Subject: [PATCH 1/4] Solution --- src/reduce.test.js | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 47a892f..62856b5 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -11,8 +11,46 @@ describe('reduce', () => { delete Array.prototype.reduce2; }); - it('should ', () => { + it('should sum array values with initial value', () => { + const arr = [1, 2, 3]; + const result = arr.reduce2((acc, val) => acc + val, 10); + expect(result).toBe(16); // 10 + 1 + 2 + 3 + }); + + it('should sum array values without initial value', () => { + const arr = [1, 2, 3]; + const result = arr.reduce2((acc, val) => acc + val); + + expect(result).toBe(6); // 1 + 2 + 3 + }); + + it('should multiply array values', () => { + const arr = [2, 3, 4]; + const result = arr.reduce2((acc, val) => acc * val, 1); + + expect(result).toBe(24); // 1 * 2 * 3 * 4 + }); + + it('should concatenate strings', () => { + const arr = ['a', 'b', 'c']; + const result = arr.reduce2((acc, val) => acc + val, ''); + + expect(result).toBe('abc'); + }); + + it('should handle single-element array with no initial value', () => { + const arr = [5]; + const result = arr.reduce2((acc, val) => acc + val); + + expect(result).toBe(5); // returns the only element + }); + + it('should handle empty array with initial value', () => { + const arr = []; + const result = arr.reduce2((acc, val) => acc + val, 100); + + expect(result).toBe(100); }); // Add tests here From e5ae3a6642b796c8d1198ec5e4c3c4551132d116 Mon Sep 17 00:00:00 2001 From: Viktoriia Pylypko Date: Sun, 20 Jul 2025 15:36:58 +0300 Subject: [PATCH 2/4] Solution --- src/reduce.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/reduce.test.js b/src/reduce.test.js index 62856b5..10035ea 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -53,5 +53,13 @@ describe('reduce', () => { expect(result).toBe(100); }); + it('should throw on empty array with no initial value', () => { + const arr = []; + + expect(() => { + arr.reduce2((acc, val) => acc + val); + }).toThrow(TypeError); + }); + // Add tests here }); From 65c7e338ea69f817a0aa4712eb68fc16af991250 Mon Sep 17 00:00:00 2001 From: Viktoriia Pylypko Date: Mon, 21 Jul 2025 18:56:31 +0300 Subject: [PATCH 3/4] Solution --- src/reduce.test.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 10035ea..5076be6 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -2,19 +2,26 @@ const { reduce } = require('./reduce'); -describe('reduce', () => { - beforeAll(() => { +beforeAll(() => { Array.prototype.reduce2 = reduce; // eslint-disable-line - }); +}); - afterAll(() => { - delete Array.prototype.reduce2; - }); +afterAll(() => { + delete Array.prototype.reduce2; +}); +describe('reduce2', () => { it('should sum array values with initial value', () => { const arr = [1, 2, 3]; const result = arr.reduce2((acc, val) => acc + val, 10); + expect(result).toBe(16); + }); + + it('should use initial value if provided', () => { + const arr = [1, 2, 3]; + const result = arr.reduce2((acc, val) => acc + val, 10); + expect(result).toBe(16); // 10 + 1 + 2 + 3 }); @@ -53,13 +60,10 @@ describe('reduce', () => { expect(result).toBe(100); }); - it('should throw on empty array with no initial value', () => { + it('should return undefined on empty array with no initial value', () => { const arr = []; - expect(() => { - arr.reduce2((acc, val) => acc + val); - }).toThrow(TypeError); + expect(arr.reduce2((acc, val) => acc + val)).toBe(undefined); }); - - // Add tests here }); +// Add tests here From f7c13fdac5e0fcbe7e6b216b705bdad01fc8f363 Mon Sep 17 00:00:00 2001 From: Viktoriia Pylypko Date: Mon, 21 Jul 2025 19:54:28 +0300 Subject: [PATCH 4/4] Solution --- src/reduce.test.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/reduce.test.js b/src/reduce.test.js index 5076be6..30e29a5 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -65,5 +65,65 @@ describe('reduce2', () => { expect(arr.reduce2((acc, val) => acc + val)).toBe(undefined); }); + + describe('edge cases', () => { + it.skip('should skip holes in sparse arrays', () => { + const arr = [1, 3]; + + arr.splice(1, 0); // тепер arr = [1, , 3] + + const visited = []; + + const result = arr.reduce2((acc, val, i) => { + visited.push([i, val]); + + return acc + (val || 0); + }, 0); + + expect(result).toBe(4); + + expect(visited).toEqual([ + [0, 1], + [2, 3], + ]); + }); + + it('should work on array-like objects', () => { + const arrayLike = { + 0: 'a', + 1: 'b', + 2: 'c', + length: 3, + reduce2: reduce, + }; + + const result = arrayLike.reduce2((acc, val) => acc + val, ''); + + expect(result).toBe('abc'); + }); + + it('should return undefined if callback is not provided', () => { + const arr = [1, 2, 3]; + + expect(arr.reduce2()).toBe(undefined); + }); + + it('should return undefined if callback is not a function', () => { + const arr = [1, 2]; + + expect(arr.reduce2(null)).toBe(undefined); + expect(arr.reduce2(123)).toBe(undefined); + expect(arr.reduce2('not a function')).toBe(undefined); + }); + + it('should handle callback throwing error (user-defined)', () => { + const arr = [1, 2, 3]; + const faultyCallback = () => { + throw new Error('Test error'); + }; + + expect(() => arr.reduce2(faultyCallback)).toThrow('Test error'); + }); + }); }); // Add tests here