jsartisan / frontend-challenges

FrontendChallenges is a collection of frontend interview questions and answers. It is designed to help you prepare for frontend interviews. It's free and open source.
https://frontend-challenges.com
20 stars 3 forks source link

Array.prototype.reduce #82

Closed jsartisan closed 2 weeks ago

jsartisan commented 2 weeks ago

Info

difficulty: medium
title: Array.prototype.reduce
type: question
template: javascript
tags: javascript

Question

Implement a custom myReduce function that mimics the behavior of the native reduce method.

The myReduce function should take an array, a callback function, and an optional initial value. The callback function should be invoked with four arguments: the accumulator, the current element, the index of the current element, and the array itself. The myReduce function should return the final accumulated value.

Use the following example to understand how the myReduce function should work:

Array.prototype.myReduce = function (callback, initialValue) {
  // Your implementation here
};

const sum = (acc, curr) => acc + curr;

const numbers = [1, 2, 3, 4];
const result = numbers.myReduce(sum, 0);

console.log(result); 
// Output: 10

Template

index.js

export const arrayReduce = (array, callback, initialValue) => {
  // your code here
};

index.test.js

import { arrayReduce } from './';

describe('arrayReduce function', () => {
  it('should sum an array of numbers with initial value', () => {
    const sum = (acc, curr) => acc + curr;
    const numbers = [1, 2, 3, 4];
    const result = arrayReduce(numbers, sum, 0);
    expect(result).toBe(10);
  });

  it('should sum an array of numbers without initial value', () => {
    const sum = (acc, curr) => acc + curr;
    const numbers = [1, 2, 3, 4];
    const result = arrayReduce(numbers, sum);
    expect(result).toBe(10);
  });

  it('should concatenate an array of strings', () => {
    const concat = (acc, curr) => acc + curr;
    const strings = ['a', 'b', 'c', 'd'];
    const result = arrayReduce(strings, concat, '');
    expect(result).toBe('abcd');
  });

  it('should handle array with one element and no initial value', () => {
    const sum = (acc, curr) => acc + curr;
    const numbers = [5];
    const result = arrayReduce(numbers, sum);
    expect(result).toBe(5);
  });

  it('should handle array with one element and initial value', () => {
    const sum = (acc, curr) => acc + curr;
    const numbers = [5];
    const result = arrayReduce(numbers, sum, 10);
    expect(result).toBe(15);
  });

  it('should throw an error when called on null or undefined', () => {
    expect(() => {
      arrayReduce(null, () => {});
    }).toThrow(TypeError);

    expect(() => {
      arrayReduce(undefined, () => {});
    }).toThrow(TypeError);
  });

  it('should throw an error when reducing an empty array without initial value', () => {
    expect(() => {
      arrayReduce([], (acc, curr) => acc + curr);
    }).toThrow(TypeError);
  });

  it('should pass the correct arguments to the callback', () => {
    const mockCallback = jest.fn((acc, curr) => acc + curr);
    const numbers = [1, 2, 3];
    arrayReduce(numbers, mockCallback, 0);
    expect(mockCallback).toHaveBeenCalledWith(0, 1, 0, numbers);
    expect(mockCallback).toHaveBeenCalledWith(1, 2, 1, numbers);
    expect(mockCallback).toHaveBeenCalledWith(3, 3, 2, numbers);
  });
});
github-actions[bot] commented 2 weeks ago

83 - Pull Request updated.