We are required to write a JavaScript function that takes in a number and returns an array of all the prime numbers that exactly divide the input number.
For example, if the input number is 18.
Then the output should be −
const output = [2, 3];
Example
Let’s write the code for this function −
const num = 18; const isPrime = (n) => { for(let i = 2; i <= n/2; i++){ if(n % i === 0){ return false; } }; return true; }; const findPrimeFactors = num => { const res = num % 2 === 0 ? [2] : []; let start = 3; while(start <= num){ if(num % start === 0){ if(isPrime(start)){ res.push(start); }; }; start++; }; return res; }; console.log(findPrimeFactors(18));
Output
The output in the console: −
[2, 3]