The main number, you need a little help

Background Information:

So, I am trying to calculate all the primes inside the number and then sum them up. sumPrimes(10)must return 17because 2 + 3 + 5 + 7 = 17.

The task is here → https://www.freecodecamp.org/challenges/sum-all-primes

I added a few console.logto show what is happening.


My approach:

My approach to this is trying to replicate this tutorial here, but in code: https://youtu.be/FBbHzy7v2Kg?t=67

What I tried with my code was to separate the number from the array from 2 to what your number (1 is not prime). 10will look like [ 2, 3, 4, 5, 6, 7, 8, 9, 10 ]. Then I use another loop forwith ito select a number from the previous array arr, add it to prime, and then remove every multiple of that number from the array ( arr), using a separate loop. When I return to get another number, we already know his prime.

The rest is if you do not understand.


Example:

The first number of this array is 2. Therefore, I add 2 to prime.

primary current value: 2

. j, , . , undefined.

(): [ 3, 5, 7, 9]

.. ( 3, 5, 7).


?

. splice, , . ?

, ES6 . (, ). , , .


https://repl.it/@John_Nicole/prime

function sumPrimes(num) {

    var prime = 0;
    var arr = [];


    for (let i = 2; i <= num; i++) {
        arr.push(i);
    } // turns into array
    console.log(arr)


    for (let i = 0; i < arr.length; i++) {
        var number = arr[i];
        console.log("Prime: "+prime+"+"+number)
        prime+=number


        for (var j = number; j <= arr.length; j+=number) {
          arr.splice(j)
        } // j

    } // i


   return "Final result: "+prime;
}

sumPrimes(10);

: D

+4
1

, splice() . MDN : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

>>> [1,2,3].splice() # Input
>>> []               # Output

:

  • ( arr)

splice :

, ,

arrayTail = arr.slice(j + 1);

,

arrayHead = arr.splice(0,j);

, ,

arr = arrayHead.concat(arrayTail);

Array.filter:

arr = arr.filter(function(value){
    // Filter all values that are divisible by the prime
    return value % number != 0;
});

:

arr = [1,2,3,4,5,6,7,8,9];
// You filter for multiples of 2
// And that okay because all the values are indices offset by 2
arr = [1,2,3,5,9];
// You filter for multiples of 3
arr[2] == 3
arr[4] == 9
// But the multiples of 3 are no longer offset by 3

, , , , .

if (arr[j] % number == 0) {
    ... your splice code
}

, , . , . , , .

, :

function sumPrimes(num) {

    var currentPrime = 0;
    var arr = [];
    var sumOfPrimes = 0;

    // Create an array of potential primes
    for (let i = 2; i <= num; i++) {
        arr.push(i);
    }
    console.log("Prime array", arr);

    // For every value in the array, remove any future multiples of it
    for (let i = 0; i < arr.length; i++) {
        var currentPrime = arr[i];
        console.log("Prime: " + currentPrime);

        sumOfPrimes += currentPrime;
        console.log("Sum of Primes: " + sumOfPrimes);

        // Remove multiples of prime
        // Start at the next value in the array
        // And loop till the end
        for (let j = i + 1; j < arr.length; j++) {
            // If the current value is a multiple of
            // the prime, then remove it
            if (arr[j] % currentPrime == 0) {
                arrayTail = arr.slice(j + 1);
                arr = arr.splice(0,j).concat(arrayTail);
                j -= 1;
                // Since you removed an element, you need to
                // move the index back so it doesn't skip
                // any checks
            }
        }

        console.log("Filtered array for ", currentPrime, arr);

    }

   return "Final result: " + sumOfPrimes;
}
+1

Source: https://habr.com/ru/post/1690358/


All Articles