DEV Community

Cover image for JavaScript Problems: Is It a Prime Number?
Tia Eastwood
Tia Eastwood

Posted on • Updated on • Originally published at tiaeastwood.com

JavaScript Problems: Is It a Prime Number?

Here is an example of a typical problem you might be given when learning JavaScript. This article is intended to help you understand the logic and structure of writing the function to solve this problem.

Create a function that returns true if a number is a prime number, and false if it is not.

Prime numbers are numbers that are only divisible by themselves and 1.
Here is a list of prime numbers for your reference.

Now we will look at how we could write the function for this and how it works:

function isItPrime(num) {

  if (num === 1) {  //if num is 1
    return false;   
  } else if (num === 2) { //if num is 2
    return true;     
  } else {
    for (let i = 2; i < num; i++) {  //i counts up from 2, until num
      if (num % i === 0) { //if num divisible by i with no remainder
        return false; //it's not a prime number
      }
    }
    return true; //else, it is a prime number
  }
}

console.log(isItPrime(23)) //true 
Enter fullscreen mode Exit fullscreen mode

So here is the logic behind it:

First we'll get 1 and 2 out of the way...

  • We rule out 1 straight away as 1 is not a prime number. So if 1 is input, this is false and so we'll return false.
  • We know that 2 is the smallest prime number, so if 2 is input, we'll return true.
  • That means if 1 or 2 were input, we'll just return the function there and don't need to waste time executing the loop

The Loop

  • The for loop will iterate through all the numbers less than the input num, and check if num is divisible by any of them.
  • If you are confused about what i means; i starts at 2 in our loop because initially we will check if num is divisible by 2, then on each iteration of the loop, it will increase by 1. So this means we can check if num is divisible by 3, or by 4, or by 5...and so on until it reaches the number before num and then stops.
  • If num is divisble by any of the numbers in the loop, then it's not a prime number, so we return false
  • If not, then it will be a prime number because it would only be divisible by itself or 1, so we can return true.

When I'm testing my code, I also like to play around with the returns to make the code more readable for myself:

function isItPrime(num) {

  if (num === 1) {  
    return num + " is NOT a prime number"   //returns: (num) is NOT a prime number
  } else if (num === 2) {
    return num + " is a prime number"      //returns: (num) is a prime number
  } else {
    for (let i = 2; i < num; i++) {  
      if (num % i === 0) { 
        return num + " is NOT a prime number"  //returns: (num) is NOT a prime number
      }
    }
    return num + " is a prime number"  //returns: (num) is a prime number
  }
}

console.log(isItPrime(23)) //prints: 23 is a prime number"
Enter fullscreen mode Exit fullscreen mode

I hope this helps you on your JavaScript journey! Figuring out the logic and how to execute it is the most confusing part of JavaScript...but I find that solving lots of these small problems helps me build up to the bigger and more complicated ones. If you keep practicing solving problems, then you'll train your brain to become better at doing it!

Peace out

Top comments (1)

Collapse
 
shaymy profile image
Shaymy

great tutorial. thank you Tia!