During the Sum All Primes challenge, I ran into an issue that I don’t understand.
function sumPrimes(num) {
if (num == 0){
return []
}
const total= [num].concat(sumPrimes(num-1))
I console .log (total) and it prints [10,9,8,7,6,5,4,3,2,1]
after that I use :
return total.reduce((sum,number,index) => {
if (number <= 3) {
sum += number ;
}
return sum;
},0);}
console.log(sumPrimes(10));
when I use that total.reduce(). it results in 0. but if i use [10,9,8,7,6,5,4,3,2,1] it prints 6, which is what I thought it would print for total since that is what the console.log for total showed earlier.
can someone explain why this is happening?
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36
Challenge: Sum All Primes
Link to the challenge: