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