1

I am trying this code

var sum = 0

for (i = 0; i < 2000000; i++) {

function checkIfPrime() {

    for (factor = 2; factor < i; factor++) {
        if (i % factor = 0) {
            sum = sum;
        }
        else {
            sum += factor;
        }
    }
}
}
document.write(sum);  

I am getting this error:

Invalid left-hand side in assignment

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Dep
  • 71
  • 2
  • 4
  • 13

6 Answers6

1

Change if(i % factor = 0) to if( i % factor == 0) and remove the function checkIfPrime() inside the for loop.

var sum = 0

for (i = 0; i < 2000000; i++) { 
   for (factor = 2; factor < i; factor++) {
      if (i % factor == 0) {
        sum = sum;
      }
      else {
        sum += factor;
      }
    }
} 
document.write(sum);

The function inside the loop is pointless.

user5478656
  • 266
  • 1
  • 2
  • 9
1

it looks like your code outputs wrong result, for example prime numbers below 6 are 2, 3 and 5, their sum is 10, your code outputs 14 in this case. Here is another code which outputs sum of primes below max value:

var sieve = [], primes = [], sum = 0, max = 5;

for (var i = 2; i <= max; ++i) {
    if (!sieve[i]) {
        // i has not been marked -- it is prime
        sum += i;
        for (var j = i << 1; j <= max; j += i) {
            sieve[j] = true;
        }
    }
}
console.log(sum);

credit to How to find prime numbers between 0 - 100?

Community
  • 1
  • 1
Andriy
  • 14,781
  • 4
  • 46
  • 50
1
function sumPrimes(num) {
  var sum = 0;
  for (var i = 2; i < num; i++) {
    if (isPrime(i)) {
      sum += i;
      console.log(sum);
    }
  }
  return sum;
}

function isPrime(num) {
  if (num <= 1) return false;
  else if (num <= 3) return true;
  else if (num % 2 == 0 || num % 3 == 0) return false;
  var i = 5;
  while (i * i <= num) {
    if (num % i == 0 || num % (i + 2) == 0) return false;
    i += 6;
  }
  return true
}

console.log(sumPrimes(2000000));
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
0

Well, I did with 250 otherwise my screen would have frozen. First of you have to single out the prime numbers after placing them inside an empty Array, which I called primeNumbers from 2 to whatever number you want. Then I create a function that would filter the prime numbers and then add them all with a reduce method inside of another variable called sum and return that variable.

  var primeNumbers =[];

    for(var i = 2; i < 250; i++){
        primeNumbers.push(i);

      }//for loop

      function isPrime(value){
        for(var x=2; x< value; x++){

        if(value % x===0){
          return false;
        }

        }//for loop
        return true;
      }//function isPrime to filter

     var sum = primeNumbers.filter(isPrime).reduce(function(acc, val) {
      return acc + val;
    }, 0);


    console.log(sum);
0

when you are using a variable inside the loop you need to declare them. You have two points in this case

  1. i is not declared
  2. factor is not declare

Your if (i % factor = 0) is wrong, as pointed by some people above.

Also, you never call the checkIfPrime() method. I don't why you created them. Also, I improved your checkIfPrime() method. Please call sumOfPrimes() method in the code below and it should work. You can modify it according to your need

function sumOfPrimes()
{
   var sum =0;
   for (var i = 0; i < 2000000; i++) 
   {

        var temp = Math.sqrt(i);

        for (var factor = 2; factor < temp; factor++)
        {
            if (i % factor === 0) 
            {
                sum += factor;
            }
        }

}
   console.log(sum);
}
Denis
  • 1,219
  • 1
  • 10
  • 15
  • You should explain more clearly why it's necessary to declare loop variables; JS by default does not require variables to be declared before use. – Nathan Tuggy Apr 17 '17 at 03:52
-1

Try changing this line if (i % factor = 0) { to if (i % factor == 0) {

ajaysinghdav10d
  • 1,771
  • 3
  • 23
  • 33