What is wrong with my code?-Sum All primes

Tell us what’s happening:

Your code so far


function sumPrimes(num) {
let arr = Array.from({length: num+1}, (v, k) => k).slice(2);
let onlyPrimes = arr.filter( (n) => {
  let n = n+1;
  while (n > 1 && n >= Math.sort(n)) {
    if ((n % m) +++ 0)
    return false;
    m--;
  }
  return true;
}); 
return onlyPrimes.reduce((a,b) => a+b);
}

sumPrimes(10);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:76.0) Gecko/20100101 Firefox/76.0.

Challenge: Sum All Primes

Link to the challenge:

I can’t understand what is wrong. Please help me. Thank you!

At the very least it looks like you have an infinite loop in there.

Maybe it would help for you to explain (to yourself and us) the big picture logical flow of your code.

You need to work through your code, step by step. Use console statements. Right off the top, I see a few problems.

What does if ((n % m) +++ 0) do? I’ve not heard of the +++ operator.

Here:

let onlyPrimes = arr.filter( (n) => {
  let n = n+1;

A variable n is being passed in to the callback function. Why are you redeclaring it with let?

Here:

while (n > 1 && n >= Math.sort(n)) {

I’m not familiar with Math.sort. Did you mean sqrt?

I haven’t even looks at the algorithm yet.

I think you need to back up. Build your app in pieces. Plan out on a piece of paper what you need to do. Build each section and test it.

I have altered my code quite a bit but I am still struggling.

Never mind, everyone! I fixed it! Thank you for helping me to think. i was suffering from an hour long brain fart.

1 Like

Trust me, we’ve all been there. The brain farts will always be there. They get more complex as you learn, but the horror/joy of coding is that there will always be things like this.

2 Likes

And learning how to solve the problem is the real lesson here.

2 Likes