Prime Number finding challenge

Hello FCC community! This is my first time posting so I hope this is the right forum - if not please feel free to point me in the right direction!
I am struggling with a coding challenge where I am asked to write code to determine whether a number is prime or not. I will post the entire challenge, cannot use a link because it requires a login and other details.
The code works for all but an odd non rime number, and the problem is in the for loop, but I’m stuck at trying to figure out why the loop will not check all numbers until the given number itself!
Thank you to anyone willing to take the time and point me in the right direction!

Write a function that takes a single positive integer as its argument. If the number is prime, it should return true . Otherwise, it should return false .

(A prime number is a number that is divisible only by itself and 1 - for example 2, 3, 5, 7, 11. The number 1 is not a prime.)

Example:

checkIsPrime(2)
// returns true

checkIsPrime(4)
// returns false
function checkIsPrime(num) {
    if (num === 1) {
        return false
    }
    if (num === 2) {
        return true
    }
    if (num % 2 === 0) {
        return false
    }
        for (let i = 2; i <= num; i ++){
        if (num % i === 0) {
        return false}
        if (num % 2 === 0){
            return false
        }
        return true
        }
    }

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

Is this a FCC challenge, or a challenge from a different site?

It’s a practice challenge from another site!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.