Intermediate Algorithm Scripting - Sum All Primes

**Tell us what’s happening: **
hello guys if need help with this algorithm i can’t! understand why when I run console.log(sumPrimes(977)) the console output is 108789 instead of sumPrimes(977) should return 73156. my logic is a bit different, but I can’t understand where the error is coming from , I will appreciate if one of you fox would help me.
Your code so far

function sumPrimes(num) {

let newArray=[];

let answer=0;

while(answer<=num){

newArray.push(answer);

answer++;}

let spliced=newArray.splice(2)

let found=spliced.filter(element=>{ if(element==2|| element==3||element==5|| element==7){

return true;

}

else if(

element%2!==0&&

element%3!=0&&

element%4!==0&&

element%5!==0&&

element%6!==0&&

element%7!==0&&

element%8!==0&&

element%9!==0

)

{

return true

}

else{

return false;

}

})

return found.reduce((accumulator,element)=>{

return accumulator+element;

},0)

}

console.log(sumPrimes(977));
```javascript
function sumPrimes(num) {
  let newArray=[];
  let answer=0;
while(answer<=num){
newArray.push(answer);
answer++;}

let spliced=newArray.splice(2)
let found=spliced.filter(element=>{  if(element==2|| element==3||element==5|| element==7){
  return true;
}
else if(
element%2!==0&&
element%3!=0&&
element%4!==0&&
element%5!==0&&
element%6!==0&&
element%7!==0&&
element%8!==0&&
element%9!==0
)
{
return true
}
else{
  return false;
}
})
return found.reduce((accumulator,element)=>{
  return accumulator+element;
},0)
}

 console.log(sumPrimes(977));

Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Sum All Primes

Link to the challenge:

This is not an accurate test for primes

Here is your code with readable formatting applied:

function sumPrimes(num) {
  const newArray = [];
  let answer = 0;

  while (answer <= num) {
    newArray.push(answer);
    answer++;
  }

  const spliced = newArray.splice(2);
  let found = spliced.filter(element => {
    if (element == 2 || element == 3 || element == 5 || element == 7) {
      return true;
    } else if (element % 2 !== 0 && element % 3 !== 0 && element % 4 !== 0 &&
               element % 5 !== 0 && element % 6 !== 0 && element % 7 !== 0 &&
               element % 8 !== 0 && element % 9 !== 0) {
      return true;
    } else {
      return false;
    }
  });

  return found.reduce((accumulator, element) => {
    return accumulator + element;
  }, 0);
}

console.log(sumPrimes(977));

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