- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Prime factor array of a Number in JavaScript
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 105.
Then the output should be −
const output = [3, 5, 7];
Example
The code for this will be −
const num = 105; 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 −
[3, 5, 7]
- Related Articles
- Finding the largest prime factor of a number in JavaScript
- k-th prime factor of a given number in java
- Find largest prime factor of a number using C++.
- Python Program for Find largest prime factor of a number
- C Program for Find largest prime factor of a number?
- Java Program to find largest prime factor of a number
- Find sum of a number and its maximum prime factor in C++
- C Program for Find the largest prime factor of a number?
- Prime digits sum of a number in JavaScript
- Is the reversed number a prime number in JavaScript
- Find all prime factors of a number - JavaScript
- Prime Factor in C++ Program
- Nearest Prime to a number - JavaScript
- Generating Random Prime Number in JavaScript
- Sum of all prime numbers in an array - JavaScript

Advertisements