code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

17 Finders Keeper

/**
 * Created by Ashish Patel
 * Copyright © 2018 ashish.me
 * ashishsushilpatel@gmail.com
 */

function findElement(arr, func) {
  let num = 0;
  for(let i = 0; i < arr.length; i++){
    num = arr[i];
    if(func(num)){
      return num;
    }
  }
  return undefined;
}

findElement([1, 2, 3, 4], num => num % 2 === 0);

Created 2019-11-24T04:53:20+05:30 · Edit