28 Apr 2023

Use findIndex to Implement Find

Taken from Execute Program

Use .findIndex to write the function find(arr, callback). It should return the first element where callback(element) is true. If the element is not found, it should return undefined.


This is the answer I came up with:

function find(arr: Array<any>, callback: any) {
  let index = arr.findIndex(callback)
  return ( index >= 0 ) ? arr[index] : undefined


Solution tested in REPL:


Copyright © Paramdeo Singh · All Rights Reserved · Built with Jekyll

This node last updated November 7, 2023 and is permanently morphing...

Paramdeo Singh Guyana

Generalist. Edgerunner. Riding the wave of consciousness in this treacherous mortal sea.

Technology Design Strategy Literature Personal Blogs
Search Site

Results are from Blog, Link Dumps, and #99Problems