
- 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
JavaScript function to take a number n and generate an array with first n prime numbers
We are required to write a JavaScript function that takes in a number n and returns an array that contains first n prime numbers. We know that prime numbers are those numbers that are only divisible by 1 and themselves like 2, 3, 19, 37, 73 etc.
We will first write a function that checks whether a given number is prime or not and then run a loop to generate n prime numbers. The code to check for prime numbers −
const isPrime = (n) => { for(let i = 2; i <= n/2; i++){ if(n % i === 0){ return false; } }; return true; };
And the full generating code will be −
Example
const isPrime = (n) => { for(let i = 2; i <= n/2; i++){ if(n % i === 0){ return false; } }; return true; }; const generatePrime = num => { const arr = []; let i = 2; while(arr.length < num){ if(isPrime(i)){ arr.push(i); }; i = i === 2 ? i+1 : i+2; }; return arr; }; console.log(generatePrime(6)); console.log(generatePrime(16)); console.log(generatePrime(36));
Output
The output in the console will be −
[ 2, 3, 5, 7, 11, 13 ] [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 ] [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151 ]
- Related Articles
- Prime numbers upto n - JavaScript
- Sum of the first N Prime numbers
- Program to generate first n lexicographic numbers in python
- Counting prime numbers from 2 upto the number n JavaScript
- Constructing an array of first n multiples of an input number in JavaScript
- Print the nearest prime number formed by adding prime numbers to N
- How to generate Prime Numbers in JavaScript?
- Find the Product of first N Prime Numbers in C++
- 8085 program to search a number in an array of n numbers
- Smallest number that is divisible by first n numbers in JavaScript
- Take two numbers m and n & return two numbers whose sum is n and product m in JavaScript
- Generate n random numbers between a range and pick the greatest in JavaScript
- How to get only the first n% of an array in JavaScript?
- How to get the first n values of an array in JavaScript?
- An Interesting Method to Generate Binary Numbers from 1 to n?

Advertisements