Error - Intermediate Algorithm Scripting: Sum All Primes

Error - Intermediate Algorithm Scripting: Sum All Primes
0

#1

Tell us what’s happening:
I was thinking that my code should be passing the tests, so I went to JS Bin (see the test here), tested it and it’s working. It doesn’t pass the challenge’s tests, and in the browser’s console I’m getting this:

TypeError: unknown: Cannot read property '0' of undefined
    at Function.e.get (commons-b48a099b457b77b5ddc8.js:32)
    at e.h [as unshiftContainer] (commons-b48a099b457b77b5ddc8.js:49)
    at t.<anonymous> (commons-b48a099b457b77b5ddc8.js:21)
    at r (commons-b48a099b457b77b5ddc8.js:49)
    at e.o [as _call] (commons-b48a099b457b77b5ddc8.js:48)
    at e.i [as call] (commons-b48a099b457b77b5ddc8.js:48)
    at e.a [as visit] (commons-b48a099b457b77b5ddc8.js:48)
    at e.visitQueue (commons-b48a099b457b77b5ddc8.js:47)
    at e.visitMultiple (commons-b48a099b457b77b5ddc8.js:47)
    at e.visit (commons-b48a099b457b77b5ddc8.js:47)

Your code so far (spoilers):

function sumPrimes(num) {
  /*
  JavaScript implementation of Sieve of Eratosthenes
  https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Pseudocode
   */

  // object with all numbers in range 2, 3, 4, ... num
  let primesObj = {};
  for (let i = 2; i <= num; primesObj[i++] = true);


  for (let i = 2; i < Math.sqrt(num); i++) {
    // if it is prime
    if (primesObj[i]) {
      // for j = i^2, i^2+i, i^2+2i, i^2+3i, ..., not exceeding num
      for (let j = 0; j < num; j++) {
        primesObj[Math.pow(i, 2) + j * i] = false;
      }
    }
  }

  let sum = 0;
  // iterate thru the object's properties
  for (let n in primesObj) {
    if (primesObj[n]) {
      sum += +n;
    }
  }
  
  return sum;
}

sumPrimes(10);

Your browser information:
Chrome:
“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36”

Firefox:
“Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0”

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes/


#2

Try console.log(sumPrimes(4)); to see where the problem lies. (It may be helpful to check the sum up to 3 and 5 as well.)

Your for loop up to the square root of num is a little off when num is a square.


#3

Thanks for pointing! The problem in those cases was this line:

  for (let i = 2; i <= Math.sqrt(num); i++) {

Specifically the <= part (the previous code didn’t check for equality).

JS Bin updated here

So, now the code works on every case, even those that aren’t being tested in the challenge, and the error is still there.


#4

I’ve found the problem.

The parser doesn’t seem to understand this line:

 for (let i = 2; i <= num; primesObj[i++] = true);

Changing the code to this, that is also valid JavaScript, it passes the tests:

for (let i = 2; i <= num; i++) {
    primesObj[i] = true;
};

Found this doing another challenge and the same error ocurred and the only thing similar was the for loop written this way.


#5

Oh! Ha, sorry, I saw that issue and helped someone with the same issue earlier and got distracted from the problem you actually posted.

(I see you’ve already found this, but I’ll leave this here anyway) the issue is being caused by your first for loop not having a block, e.g., you have for (conditions); instead of

for (conditions) {
  // things to do in the for loop
}

and so the code is choking on that. I’ve read that this can be done if you are not following the loop with another for loop, but not if it is immediately followed by another for loop. It worked in jsbin, so I guess that’s not entirely true, but changing this in the FCC window stopped the error being thrown, so this may an error in only some (older?) version of JavaScript (maybe?).