Sum All Primes in JAvascript challenges

Tell us what’s happening:
Are my breaks functioning as i expect them too? My first break should be breaking out of the while loop while my second one should break out of the for loop. My algorithm pushes all num from 2 to 10 into the array.

Your code so far


function sumPrimes(num) {

var primeArray = [];
var possiblePrime = true;
while (num >= 2){
  if(num === 2){
    primeArray.push(2);
    // should break out of while loop
    break;
  }
  for(let divisor = 2; divisor < num; divisor++){
    if(num % divisor === 0){
      possiblePrime = false;
      // should break out of for loop
      break;
    }
  } 
  if(possiblePrime = true){
    primeArray.push(num);
  }
  // decrement num and reset prime to true;
  num--;
  possiblePrime = true;
  }

console.log(primeArray)
  return primeArray.reduce((x,y) => x+y);
}


console.log(sumPrimes(10));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

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

if(possiblePrime = true){
    primeArray.push(num);
  }

You are setting the value of possiblePrime to true instead of checking if it is true

2 Likes