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
function findPrimes(n) {
  const primes = [];
  let num = 2;

  while (primes.length < n) {
    let isPrime = true;

    // Check if num is prime
    for (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:

index.tsx
const firstTenPrimes = findPrimes(10);
console.log(firstTenPrimes); // [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
108 chars
3 lines

gistlibby LogSnag