Task of Challange #18
Print the first 99 prime numbers
Solution
Explanation
The function printPrimes(nPrimes) is our looping function that will print out the number of primes we pass in. We stick to 99 for this challenge.
The next step is to set n to 0, because we want to start with the 0th prime number and set i to 2, because we know the first prime number must be 2.
Then we loop over the condition of n < nPrimes and check if the value of i is a prime number by actually calling our second function: isPrime(number).
This function is all about checking if the number is below 2; no prime number. Then if it is equal to 2; a prime number. For every other case, we apply the commonly known rules for prime numbers. Being divideable only by itself or 1.
The for loop checks if we have a maxdivisor that results in the valid integer division with the result of 0. If so, it isn’t a prime number; referring back to the commonly know rules for prime numbers.
If the loop does not return false and run through successfully, the function returns true, and the while loop of printPrimes continues.
The printout for the first 18 numbers. The list goes on until 99. The output means the 0th prime number is 2, then the 1st prime number is 3, and so on…
Follow me on Instagram and don’t miss the latest Challenge!
https://www.instagram.com/arnold.code/
Become a bullet proof developer and seek my coding adventures on Udemy
https://www.udemy.com/user/arnold-abraham-3/
Don’t Miss My Most Read Article with over 8000 Views
One Reason Not to Migrate From JavaScript to TypeScript
Typescript saves a lot of development time, but is it worth an entire migrating process?
Get Your JavaScript Coding To A Very Professional Level with this article
Upgrade Your Legacy Coding Style With These 6 Modern Ways of Coding
And How These 3 Famous TV Shows Make You Write Excellent JavaScript Code