Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer is that the inner loop is unnecessary in your solution. Your recursive function is already doing the work of the inner loop. What happens in the inner loop is that some numbers are all repeated more than necessary. </p> <p>Dygestor's solution is one approach. The other approach is simpler</p> <pre><code>for(var i = 3; i &lt;= 10; i++) { isPrime(i, 0); } </code></pre> <p>The logs for your method:</p> <pre><code>Check 1: i is 3, j is 0 and k is 1 3 is a prime total so far is 3 [2, 3] is P Check: i was 3, j was 0 and k was 0 Check 1: i is 4, j is 0 and k is 2 Not P Check: i was 4, j was 0 and k was 2 4 is not a prime Check 1: i is 5, j is 0 and k is 2 Check 2: i is 5, j is 1 and k is 2 5 is a prime total so far is 8 [2, 3, 5] is P Check: i was 5, j was 1 and k was 1 Check 1: i is 6, j is 0 and k is 3 Not P Check: i was 6, j was 0 and k was 3 6 is not a prime Check 1: i is 7, j is 0 and k is 3 Check 2: i is 7, j is 1 and k is 3 Check 3: i is 7, j is 2 and k is 3 7 is a prime total so far is 15 [2, 3, 5, 7] is P Check: i was 7, j was 2 and k was 2 Check 1: i is 7, j is 1 and k is 2 Check 3: i is 7, j is 2 and k is 2 Check 4: i is 7, j is 3 and k is 2 Not P Check: i was 7, j was 3 and k was 2 7 is not a prime Check 1: i is 7, j is 2 and k is 3 Check 4: i is 7, j is 3 and k is 3 Not P Check: i was 7, j was 3 and k was 3 7 is not a prime Check 1: i is 8, j is 0 and k is 4 Not P Check: i was 8, j was 0 and k was 4 8 is not a prime Check 1: i is 9, j is 0 and k is 4 Check 2: i is 9, j is 1 and k is 4 Not P Check: i was 9, j was 1 and k was 4 9 is not a prime Check 1: i is 10, j is 0 and k is 4 Not P Check: i was 10, j was 0 and k was 4 10 is not a prime 15 [2, 3, 5, 7] </code></pre> <p>The logs removing the inner loop (bear in mind this is missing the <code>console.log</code> inside the inner loop as well):</p> <pre><code>3 is a prime total so far is 3 [2, 3] is P Check: i was 3, j was 0 and k was 0 Not P Check: i was 4, j was 0 and k was 0 4 is not a prime Check 2: i is 5, j is 1 and k is 0 5 is a prime total so far is 8 [2, 3, 5] is P Check: i was 5, j was 1 and k was 1 Not P Check: i was 6, j was 0 and k was 1 6 is not a prime Check 2: i is 7, j is 1 and k is 0 Check 3: i is 7, j is 2 and k is 0 7 is a prime total so far is 15 [2, 3, 5, 7] is P Check: i was 7, j was 2 and k was 2 Not P Check: i was 8, j was 0 and k was 2 8 is not a prime Check 2: i is 9, j is 1 and k is 0 Not P Check: i was 9, j was 1 and k was 0 9 is not a prime Not P Check: i was 10, j was 0 and k was 1 10 is not a prime 15 [2, 3, 5, 7] </code></pre> <p>Also the <code>k</code> variable is unnecessary in the new solution. <code>j</code> will increment until it reaches the end of the primes array or until the target number is divisible, whichever is sooner.</p> <p>If you want to use the inner loop solution, you will need to remove your recursive function and do something like:</p> <pre><code>var primes = [2]; var sum = 0; // Start looping for(var i = 3; i &lt;= 10; ++i) { var prime = true; // Prime until proven innocent for(var j = 0; j &lt; primes.length; ++j) { // Length stays same until later if(i % arr[j] === 0) { // The meat of your isPrime function: divisible? prime = false; break; // Stop the loop early: number is not prime! } } if(prime) { // We have a prime! primes.push(i); // Add it to our list of primes sum += i; // Add the prime to the sum } } // Log the result console.log("The sum of primes up to 10 (inclusive)", sum); console.log("These primes were", primes); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload