Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Array.prototype.reduce #103

Open
3 of 8 tasks
loia5tqd001 opened this issue Jan 8, 2024 · 1 comment
Open
3 of 8 tasks

Issue with Array.prototype.reduce #103

loia5tqd001 opened this issue Jan 8, 2024 · 1 comment

Comments

@loia5tqd001
Copy link

loia5tqd001 commented Jan 8, 2024

Your GreatFrontEnd account email (optional)

loia5tqd001@gmail.com

Problematic section

  • Question Description
  • Question Skeleton
  • Question Solution
  • Test Cases (Missing/Incorrect/Unclear)

Type of question

  • JavaScript
  • User Interface
  • System Design
  • Quiz

Describe the issue

Missing testcase. The code below passed all the tests but it behaves differently with the original reduce function. Pls add a testcase to catch this bug.

Code you used to submit (where relevant)

/**
 * @template T, U
 * @param {(previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U} callbackFn
 * @param {U} [initialValue]
 * @return {Array<U>}
 */
Array.prototype.myReduce = function (callbackFn, initialValue) {
  const hasInitial = initialValue !== undefined;
  if (!hasInitial && !this.length) throw new TypeError();

  let result = hasInitial ? initialValue : this[0];
  const startIndex = hasInitial ? 0 : 1;
  for (let i = startIndex; i < this.length; i++) {
    if (Object.hasOwn(this, i)) {
      result = callbackFn(result, this[i], i, this);
    }
  }
  return result;
};

console.log(
  [,2,3].myReduce((a, b) => a + b), 
  [,2,3].reduce((a, b) => a + b)
);

Edit: Later I realized that this code is similar to the solution 😃, so I marked Question Solution as the problem as well.

@loia5tqd001
Copy link
Author

Another code that passed all the testcases but behaves differently with the original reduce

/**
 * @template T, U
 * @param {(previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U} callbackFn
 * @param {U} [initialValue]
 * @return {Array<U>}
 */
Array.prototype.myReduce = function (callbackFn, initialValue) {
  if (initialValue === undefined && !this.length) throw new TypeError();

  let result = initialValue;
  for (let i = 0; i < this.length; i++) {
    if (!Object.hasOwn(this, i)) continue;
    if (result === undefined) {
      result = this[i];
      continue;
    }
    result = callbackFn(result, this[i], i, this);
  }
  return result;
};

console.log(
  [,2,3,4,5,6].myReduce((a, b, i) => i === 2 ? undefined : a + b, 0), 
  [,2,3,4,5,6].reduce((a, b, i) => i === 2 ? undefined : a + b, 0)
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant