# Sum of all prime numbers <= entered number

Dear Code Community!

I am struggling with the mentioned task, although my code seems to returned the desired output. I wonder if somebody could give me a hint what I did wrong?

I checked whether I returned a number (yes) and whether that number is the sum of all prime numbers equal or lower to the entered number, which is also true (for the values I tested…)

``````const arrPrimes = []

function sumPrimes(num) {

if (num == 1) {
let arrSummed = arrPrimes.reduce((a, b) => a + b);
console.log("What's returned: " + arrSummed + ", typeof:", typeof arrSummed);

return arrSummed;

}

//console.log("Invalid input: " + num + " is not a prime number by definition!")};
let i = 2;
while (num % i != 0) {
i++
}

if (i == num) {
// console.log(`\${i} is a prime number.`)
arrPrimes.push(i);
// console.log("Content of arrPrimes:", arrPrimes)
}

// else {console.log(num, "is not a prime number.")}

sumPrimes(--num);

}

sumPrimes(17)
``````

This is a global variable. What happens when you run the function twice in a row?

Also, where is your return statement?

You can bully this into working with recursion, but recursion is not a good fit for this problem.

The Output is wrong because the global Array remains changed after the first call.
But where can I put the array inside of the function so it does not get emptied and overritten with the next prime number in line when the recursive call happens?

How can I prevent that from happening?

When working with recursion, you should use the return value of the recursive function call - but this really isn’t a great fit for recursion. It’s especially messy to use recursion on a function that returns a number while also wanting to pass around an array. You have to do some clever hacks that make your code harder to follow.

Edit: I was able to get it to work with a default parameter, but it still isn’t a super efficient approach.

@JeremyLT I didn’t fully grasp what you passed as default parameter, an empty array as second argument? I couldn’t make it work yet.

I managed to clear the array in the base case by splicing it before returning the desired number, it still won’t work… although calling the function a few times always gives me the right result (so somewhat of an improvement…)

``````if (num == 1) {

let arrSummed = arrPrimes.reduce((a, b) => a + b);

console.log("What's returned: " + arrSummed);

arrPrimes.splice(0,arrPrimes.length);

return arrSummed;

}
``````

Any further hints?

@JeremyLT I now replaced the recursion with a while-loop, it worked, but it still would be interesting to know why my code above doesn’t work and how you made my recursive code work!

You can’t get this to work with a global variable, it needs to be a local variable.

The function signature I got to work with the recursive passing around the array was

``````function sumPrimes(num, primesArr = []) {
``````

though, there is no need to make this array. If you are making an array just to sum its contents, then your really should just be keeping track of the sum as you go.

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