Tell us what’s happening:
I get error with this task when inputs above certain number, returns incorrect result:
- I’ve tested in chrome console and result from 977 correctly results with 73156, meaning my code should be fine.
- The result in FCC console each time returns different result: 8275, 7141, 5117 etc.
From what I understand, for some unknown reason, the loop breaks before reaching 977 and returns whatever is there. I would guess it would be to limit harmful while loops or smth…
Your code so far
function sumPrimes(num) {
let i = 0;
let primesArr = []
while(i<=num){
let divides = []
for( let j=0; j<num; j++){
if(i%j===0){
divides.push(true)
}
}
// console.log(i, divides)
if(divides.length<=2){
primesArr.push(i)
}
i++
}
// console.log(primesArr)
let sumArr = primesArr.reduce((a,b) => a + b, 0)
console.log(sumArr-1)
return num;
}
sumPrimes(977);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.
Link to the challenge: