2 being spliced

Tell us what’s happening:
Why is the ‘2’ being spliced when it doesn’t satisfy the condition in the if statement in the second ‘for’ loop?

  **Your code so far**

function sumPrimes(num) {
let total = [];
for (let i = 2; i <= num; i++) {
  let qqq = total.push(i);
};

let newAnswer = total.slice();
for (let p = 0; p < total.length; p++) {
  if (num % total[p] !== 0) {
    newAnswer.splice(p, 1);
  };
};
console.log(newAnswer);
};


sumPrimes(10);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36

Challenge: Sum All Primes

Link to the challenge:

add inside the loop console.log({newAnswer, total}) and see what happens to the two inside the loop

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.