frontend-challenges icon indicating copy to clipboard operation
frontend-challenges copied to clipboard

145 - minBy - typescript

Open jsartisan opened this issue 1 year ago • 0 comments

index.ts

export function minBy<T>(array: T[], iteratee: (item: T) => number): T | undefined {
  if (array.length == 0) return;

  return array.reduce((previous, current) => {
    const previousValue = iteratee(previous);
    const currentValue = iteratee(current);

    if (previousValue > currentValue) {
      return current;
    }

    return previous;
  });
}

jsartisan avatar Aug 09 '24 11:08 jsartisan