Reduce() Method for Array

Good day,

I need your wise pearls of wisdom. I am not sure why an undefined is returned. The objective of the challenge is to write a function that returns the sum of elements greater than 5 in the given array. However, instead of returning the sum, the function returns Undefined. Can someone please kindly explain why it is returning undefined. I used the .reduce() method to solve it. Thanking you in advance.

For example,
sumFive([1, 5, 20, 30, 4, 9, 18]) ➞ 77

function sumFive(array){
let theSumm = array.reduce(function(val,currVal){
if(val>5){
return (val+currVal)
}
})
return theSumm
}

Which variable do you want to check is greater than 5?

Shouldn’t your reducer callback function always return a number?

Hey! Thank you for your response.

I wanted to check all of the variables (elements) in the array. I wanted to return the sum(total) of all of the number which are greater than 5 in the array. So yes, I was expected to return a number but instead an “undefine” is returned.

Sorry, I might not have been as clear as I should have been. I was using those questions as a way of telling you where your issues are without just telling you what your issues are :slight_smile:

What does val represent? What does currVal represent? If you can answer that then I think you will figure out the first issue.

My second question was trying to let you know that your reducer callback function must always return a number. Is your function always returning a number? And if it doesn’t, then what value does a function return if you don’t explicitly return a value?