
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Find all prime factors of a number - 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 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]
- Related Articles
- Program to find all prime factors of a given number in sorted order in Python
- C Program for efficiently print all prime factors of a given number?
- How to find prime factors of a number in R?
- Java Program to find Product of unique prime factors of a number
- Prime factors of a big number in C++
- Python Program for Efficient program to print all prime factors of a given number
- C/C++ Program to find Product of unique prime factors of a number?
- C/C++ Program to find the Product of unique prime factors of a number?
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X in C++
- Maximum number of unique prime factors in C++
- Count number of factors of a number - JavaScript
- Product of unique prime factors of a number in Python Program
- Python Program for Product of unique prime factors of a number
- Find all the prime factors of 1729 and arrange them in ascending order. Now state the relation, if any; between two consecutive prime factors.
- Listing all the prime numbers upto a specific number in JavaScript

Advertisements