2023-04-28

 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.

Solution

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
}

Testing

Solution tested in REPL:

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMGYAmAKAhgJxwLkQEE8sBPAHizDID4AaRCLAGxYCMsIBrQ6sgJSIA3gFgAUIkQsAplESo0MgB6IAvIlw4AdCnQBJdCozM2nHgIlScckDiQYFR1bQ0AGREID8mvAG1FFQBdREJwJT0ZNAkAXwkJCAQAZzhZbRY4AHMMKylkRQw-AEZGACZGAGYgxgwwQjAQAFt2GRwhNVpEJDUexCKBRgB6Qb7cqT1MPwBWRgA2RgB2asRa+qaWtvVOpBHS9V6KgcRhxFmx-PRCksRyxCqauq711vbtxE6po5PwmUjoyTyE0Ky1WT2aLy2XXeiE+QxGPz+5yBfgabBBj34ryhPQ0qJYXxGeIklnECWS8i06kQxTKQQkWj8AFoiiENBUyWAUmkMtkkQUtA81uDNh1sb1Ycd4UZEeISUA

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