Factorialize a Number: a unreadable problem about the usage of reduce

this is my first solution to challenge:

function factorialize(num) {
   var arr=[];
function factorialize(num) {
  for (var i=num;i>0;i--){
    arr.push(i);
  }
var result = arr.reduce(function(previousVla,currentVla){
  return previousVla * currentVla;
},1);
 }  
}
factorialize(5);

But the result is noting like this:
.
I have passed this challenge,but still be troubled about the reason why reducedon’t work.

Looking at it closely, all your (outer) function does is create an empty array, then create a function with the same name as the outer function. No matter what data you passed it, it will always return undefined. It’s not that reduce didn’t work. it’s just that the function that contains that .reduce wasn’t called in the first place.

1 Like

your main function is called factorialize and then you create another function inside with the same name … give the inner one a different name .eg factorial

the inner function has no return statement … right under where you have var result put …return result

under that is your closing bracket for your inner function … so under this your in your outer function and you have no return statement so put … return factorial(num) … or whatever you name your inner function …
this now calls your inner function with the number and the inner function returns the result and your outer function returns this result

thanks. it’s my stupid fault.i should check the whole code more carefully.:sweat: