FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Max By

10mins

Implement a function maxBy(array, iteratee) that finds the maximum element in an array based on the specified criteria.

The function should:

  • Accept an array and an iteratee function.
  • Apply the iteratee to each element to generate a criterion for comparison.
  • Return the element that has the maximum criterion value.
  • Return undefined if the array is empty.

Examples

maxBy([1, 2, 3, 4], x => x); // 4 maxBy([{ n: 1 }, { n: 2 }], o => o.n); // { n: 2 } maxBy(['apple', 'banana', 'kiwi'], s => s.length); // "banana" maxBy([], x => x); // undefined

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests