Print prime numbers using Javascript functional approach

function isPrime(n){
   if(n < 2) return false; 
    
   for(var i = 2 ; i < n ; i++){
       if(n % i === 0) return false; 
   }
   return true; 
}   

var primeList = [-1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].filter(isPrime);
console.log(primeList);

Output

2, 3, 5, 7, 11

Comments