FindFirstPrimeAsync

by Matthew Vasallo

JavaScript

/* 
 
 Write a method that finds the first prime number that repeats in a list of integers.
 
 
Example
[6, 3, 2, 5, 8, 3] // should return 3
[6, 3, 5, 2, 5, 8, 3] //should return 5
[4, 4, 11, 2, 5, 8, 11, 5] //should return 11
[1,2,3,4,5] // should return false
 
 
*/

const isPrime = num => {
  for (let i = 2, s = Math.sqrt(num); i <= s; i++)
    if (num % i === 0) return false;
  return num > 1;
};

const dataUnderTest = [6, 3, 2, 5, 8, 3];

const isPrimeAsync = num => {
  return new Promise((res, rej) => {
    res(isPrime(num))
  })
};


//CODE TO CHANGE IS BELOW
const findFirstPrime = () => 1;

const findFirstPrimeAsync = () => Promise.resolve(1);


//CODE TO CHANGE IS ABOVE
console.log(`Sync First Prime Is: ${findFirstPrime(dataUnderTest)}`);

findFirstPrimeAsync(dataUnderTest)
  .then((result) => console.log(`Async First Prime Is: ${result}`));