a python function that calculates the first prime numbers in javascript
Here's an example Javascript function that calculates the first n prime numbers:
index.tsx
functionfindPrimes(n) {
const primes = [];
let num = 2;
while (primes.length < n) {
let isPrime = true;
// Check if num is primefor (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(num);
}
num++;
}
return primes;
}
361 chars
25 lines
To use this function, simply call it with the desired number of primes to find: