I tried doing the exercise with .reduce to get the sum until it was <= num but it didn’t work. I have noticed in the different answers that they first get the array with the odd numbers that meet the condition and then add it.
What I was trying to do is apply .reduce to an array with odd numbers <= num (perhaps it was not the focus of the exercise) but it raised me the doubt that if .reduce can be stopped when meeting a condition.
Thank you, sorry for my English and I am a beginner.
When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
I can see two ways to use reduce. One way would be what you have here - filtering out the evens. Of course, you could also use a filter method for that. The other option would be to just use reduce and if the current is odd, return the sum of the accumulator and current - if the current is even, just return the accumulator - that would keep the even numbers out of the sum because only odds are added to the accumulator.