my doubt is the following I have this function and when I use reduce to obtain the greater and lesser number when I use the arrow function as in greater it gives me the result but when a regular function gives me undefined... but if I let less I take it out of the function sumMajorMinor2 works fine... why is this? thank you very much for your answers in advance
function sumaMayorMenor2(arr){
console.log(arr)
let menor = arr.reduce(function(num3,num4) {if(num3>num4){
return num3=num4
}})
let mayor = arr.reduce((num1,num2)=> num1<num2 ? num1=num2 : num1)
console.log(mayor,menor)
return(console.log(menor+mayor))
}
The problem is not in how you define it (it works with both methods), the problem is that the minor function does not do what it should, because in case the number num3 is minor, you are not returning anything. You can correct it like this:
Or better, if you want them compact, like this: