# Sum All The Primes

Hey guys here is the link to the current algorithm challenge I’m working on.

My code before works, now I’m just rewriting the code to the way that I can understand. It was passing earlier all I did was change the format of the if or else statement. I have a feeling it’s a syntax issue with the brackets or scope? Can anyone pinpoint or see an issue with this code? See below.

``````function sumPrimes(num) {
function checkPrimes(number){
for (let i = 2; i<number; i++){

if(number % i === 0){
return false
}else{
return true;
}

}
}
for (let j = 2; j<= num; j++){
if(checkPrimes(j)){
}
}
}
sumPrimes(10);
``````

The issue is here. What happens when i===2? (hint: can it keep looping?)

1 Like

Look at the location of this return… When can you actually return true? (Formatting applied for clarity)

Side note: syntax highlighting is showing `number` to be a keyword, so a bad variable name:

1 Like

2/2 =1, it’s still a prime number so it’s still iterating to the next one?

What does `return` do?

Are you sure? If it is divisible by two, it returns one thing immediately - if not, it returns the other. Also immediately.

Either way, the loop doesn’t continue.

1 Like

You can return true… How come writing the code this way pass?

``````
function sumPrimes(num) {
function checkPrimes(number){
for (let i = 2; i<number; i++){
if(number % i === 0){
return false
}
}return true
}
for (let j = 2; j<= num; j++){
if(checkPrimes(j)){
}
}
}
console.log(sumPrimes(10));
``````

I’ve edited your post 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 (’).

when, specifically, is that true being returned? A key thing has happened, making true the right answer.

1 Like

Right click and use the `Format Document` option.

Doing this will make the logic in the code much, much clearer.

In this case, I also moved your return statement to a new line for clarity.

``````  function checkPrimes(number) {
for (let i = 2; i < number; i++) {
if (number % i === 0) {
return false
}
}
return true
}
``````

In this case, you are returning true once the loop completes. You can’t say a number is prime before checking all possible divisors.

Notes:

• `number` is still a bad variable name

• You don’t need to check all the way up to `number` to know if it is prime

2 Likes

Jeremy thanks for the clarification. Also, my total bad for now using the back-ticks. Wasn’t sure what the formality was here. Okay that makes sense, why do i get an unknown unexpected token if I put else return true vs return true?

Where are you putting the `else` statement? An `else` can only come after a code block immediately following an `if` statement. In the code you just posted, the `return true` comes outside of the `for` loop’s body.

1 Like

That is true, but I also tested it out by writing it like this
`for (let i = 2; i<number; i++){ if(number % i === 0){ return false } } else return true `

Mangling the spacing does not change the logic. You can’t put an else statement after the closing brace of a for loop. It just doesn’t go there.

I’m not sure that you understand loops and if statements fully.

This loop, as written, checks every value of i less than number. If any of those values divide evenly into the number, then it returns false. If the entire loop completes without returning false, then no value divides evenly into your number and you can return true.

1 Like

Got it. That’s just the way my logic/brain works. “If this happens… do this…Else do that…”

1 Like

Sure, but else is a special control statement. It cannot be used independently from an if statement.

2 Likes

Hey you’re right the loop does stop because it’s divisible by two. Return execution stops when the condition is met!

1 Like

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