0

I'm trying to store all prime numbers of a given number in an array. I'm running an inner loop from 0 to the count of the outer loop every time the outer loop increases by one. I have if statements inside the inner loop to determine if the number is prime.

function sumPrimes(num) {
  var primes = [];
  for (var i=0; i<num; i++){
   for (var j=0; j<num[i]; j++){
     if(num[i][j] !== 1){
       if(num[i] % num[i][j] !== 0){
         primes.push(num[i]);
       }
     }
   }
  }
  return primes;
}

sumPrimes(10);

Currently this isn't returning anything. What is the correct way of targeting the counters? essentially I want to say - if (the [number of the outer loop count] is NOT divisible by any number bar 1 and itself from the inner loop that leaves no remainder) { push this step on the outer loop into my 'primes' array }

Edit: im not seeking better solutions on how to fill up a prime number array of which im sure there is many ill learn after I solve this. I am right now using this problem as an example for nesting for loops and how to correctly call the different parameters.

  • Possible duplicate of [How to find prime numbers between 0 - 100?](https://stackoverflow.com/questions/11966520/how-to-find-prime-numbers-between-0-100) – Scott Marcus May 26 '17 at 19:15
  • Hi Scott, my question is seeking a better understanding of nesting for loops in javascript. I'm not asking for better solutions – Phillip Smith May 26 '17 at 19:22
  • 1
    `num` is `10` in your example. The rest of your code works based on `num[i]`, which attempts to get a value at an index, which doesn't work with a number. So, everything you are doing is essentially wrong. – Scott Marcus May 26 '17 at 19:50
  • Every time you write `num[i][j]`, exchange it for `j`. Every time you write `num[i]`, exchange it for `i`. The result will be grossly wrong, but you will have accessed the counter variables. – ccprog May 27 '17 at 01:18

0 Answers0