Task of Challenge #16/54

Create a function that will return a Boolean specifying if a number is prime.

Test with 1,5,6,7,9,11,13 & 27.

Solution

Explanation

What is a prime number in maths?

Prime numbers are special numbers, greater than 1, that have exactly two factors, themselves and 1. 19 is a prime number. It can only be divided by 1 and 19. 9 is not a prime number.

In our solution, we first check if the passed number is below 2. Then we already checked against any number below 2 being no prime number.

If the number is equal to 2, then it is a prime number and we can return true.

If any number passed both checks and we are still inside our function, then we find the maximum divisor by getting the square root of our passed number.

With a for loop, we check if the number is remainderless dividable by the iterator of i. If the remainder is 0, then we have definitely another valid calculation instead of only being able to divide a number with 1 and itself to be a prime number. Therefore, we are returning false.

Testifying the given numbers results into this printout.

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