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
}
}