Node.js lodash filter() find prime numbers

Description

Node.js lodash filter() find prime numbers


/* Program 5: Find a prime number in the list */
let lodash=require('lodash');
let numbers = [1,2,3,4,5,16,18,19]

// following is a code to find prime numbers in list using filter, any
function isPrime(number) {
    if(number<=3) {
        return true;
    } else {/* w  ww  .j av  a2 s.c o m*/
        return !isDivisble(number);
    }
}

function isDivisble(number) {
    let divisors = lodash.range(2, number);
    return lodash.any(divisors, (divisor) => number % divisor === 0)
}

console.log(lodash(numbers).filter(isPrime).value())



PreviousNext

Related