Guest User

Untitled

a guest
Apr 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. // const inputArr = [4,1,3,14,15,18,39,56,89,101,150,165,187] // output 3
  2. // const inputArr = [9,31,38,5,62,44,38,17,19,38,50,74] // output 5
  3. const inputArr = [5,6,8,9,11,14,16,18,20,25] //output 'null'
  4.  
  5. const smallestPrime = (numArray) => {
  6. const inputArr = numArray.slice();
  7. const isPrime = num => {
  8. for(let i = 2, s = Math.sqrt(num); i <= s; i++)
  9. if(num % i === 0) return false;
  10. return num !== 1;
  11. }
  12. const isIndexEvenPosition = num => (inputArr.indexOf(num) + 1) % 2 !== 0
  13. const primeNumArray = numArray.filter(isPrime)
  14.  
  15. let smallestNumPosition = 0 // copy input array
  16. let smallestNum = primeNumArray.sort((a,b) => a-b)[smallestNumPosition]
  17.  
  18. while (isIndexEvenPosition(smallestNum)) {
  19. smallestNumPosition++;
  20. smallestNum = primeNumArray[smallestNumPosition]
  21.  
  22. }
  23.  
  24. return smallestNum > 0 ? smallestNum : 'NULL'
  25. }
  26.  
  27. console.log(smallestPrime(inputArr))
Add Comment
Please, Sign In to add comment