How to find the index of first prime number in array using callback function and Array.findIndex() in JavaScript

1 Answer

0 votes
function findPrime(element, index, array) {
  var i = 2;
  while (i <= Math.sqrt(element)) 
  {
    if (element % i++ < 1) 
      return false;
  }
  return element > 1;
}

arr = [1, 6, 7, 10, 13, 29];

console.log(arr.findIndex(findPrime)); 


/*
run:  
 
2
  
*/

 



answered May 21, 2016 by avibootz
...