Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
* @returns {*}
*/
function reduce(callback, startValue) {
let prev = startValue;
let prev;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential issue: The function relies on this being an array, but if reduce is called as a standalone function (not as a method of an array), this will be undefined or incorrect. Consider clarifying in your documentation or usage that this function must be called with an array context, or refactor to accept the array as an explicit argument if required by the task description or checklist.

let startIndex = 0;

if (arguments.length < 2) {
startIndex = 1;
if (this.length === 0) {
throw new TypeError('Reduce of empty array with no initial value');
}

prev = this[0];
startIndex = 1;
} else {
prev = startValue;
}

for (let i = startIndex; i < this.length; i++) {
Expand Down
49 changes: 47 additions & 2 deletions src/reduce.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-len */
'use strict';

const { reduce } = require('./reduce');
Expand All @@ -11,9 +12,53 @@ describe('reduce', () => {
delete Array.prototype.reduce2;
});

it('should ', () => {
it('should sum up numbers with initial value', () => {
const result = [1, 2, 3, 4].reduce2((acc, val) => acc + val, 10);

expect(result).toBe(20);
});

// Add tests here
it('should sum up numbers without initial value', () => {
const result = [1, 2, 3, 4].reduce2((acc, val) => acc + val);

expect(result).toBe(10);
});

it('should concatenate strings', () => {
const result = ['a', 'b', 'c'].reduce2((acc, val) => acc + val, '');

expect(result).toBe('abc');
});

it('should multiply numbers without initial value', () => {
const result = [2, 3, 4].reduce2((acc, val) => acc * val);

expect(result).toBe(24);
});

it('should use array index and array as arguments', () => {
const spy = jest.fn((acc, val, i, array) => acc + val);
const arr = [1, 2, 3];

arr.reduce2(spy, 0);
expect(spy).toHaveBeenCalledWith(0, 1, 0, arr);
expect(spy).toHaveBeenCalledWith(1, 2, 1, arr);
expect(spy).toHaveBeenCalledWith(3, 3, 2, arr);
});

it('should work on array with single element and no initial value', () => {
const result = [42].reduce2((acc, val) => acc + val);

expect(result).toBe(42);
});

it('should throw error on empty array with no initial value', () => {
expect(() => [].reduce2((acc, val) => acc + val)).toThrow();
});

it('should return initial value on empty array if initial value provided', () => {
const result = [].reduce2((acc, val) => acc + val, 100);

expect(result).toBe(100);
});
});