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;
}
}
}
let answer = 0;
for (let j = 2; j<= num; j++){
if(checkPrimes(j)){
answer+=j;
}
}
return answer;
}
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.
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.
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.