Trying to find factors of a number in JS

前端 未结 13 2804
陌清茗
陌清茗 2021-02-19 04:52

I am just starting JS, and understand the concept of finding a factor. However, this snippet of code is what I have so far. I have the str variable that outputs nothing but the

13条回答
  •  借酒劲吻你
    2021-02-19 05:43

    function factorialize(num) { 
     if(num === 0)
       return 1; 
     var arr = []; 
     for(var i=1; i<= num; i++){
       arr.push(i);
     } 
     num = arr.reduce(function(preVal, curVal){
       return preVal * curVal;
     });
    
      return num;
    }
    
    factorialize(5);
    

提交回复
热议问题