Factorialize a Number. Messy solution?

Hi, could someone look at my code please. I can pass the test but i think my solution may be a bit messy. Could someone let me know please?

Also, am i missing something?, why return a 1 from 0. 1 is not the factor for 0, I asked google. :slight_smile: this requirement is what is making the code look messy i think.

Cheers

Mark

 function factorialize(num) {
      var numbers = [];                 //empty array to fill
      var high = num;                   //sets (num) as "high" number
      var low = 0;                        //sets iteration stop
      
      
      //if else fills challenge requirement of returning 1 from 0 only
      if (num > 0)

      //creates an array by iterating (num)
      for (var i = high; i > low; i--){
      numbers.push(i);
          }
        
      //multiply array items to get factor
      factored = numbers.reduce(function(high, low) {
      return low * high;
      });
      return factored;
         
      }
      
      else { 
        return 1;
      }
    }


    factorialize(20);
1 Like

Make special case for 0; for other imputs just iterate from 2 to num multiplying the result by iterator.

Thank you, great post, I understand now, .

I have also taken the time to understand your code snippet and I think its sank in.

Thnaks again

Mark

Can you please take a look at my code? My logic is a bit similar to yours, but I count it up although you count down. Why doesnt this work? Is it because of i? But how? Thank you :smiley:
function factorialize(num) {
if(num>0){
while(i<num-1){
product*=i;
i++;
}
console.log(product);
}
else {
return 1;
}
}
factorialize(5);

Looks like you didn’t initialize i.

1 Like

This is probably not the complete code but:

  • Initialise all your variables to the correct default values. ‘i’ should start at 1, product at 1 as well.
  • Loop till num not num - 1. 5! = 54321 not 432*1.
1 Like

thank you guys so much ^^

Numberphile shows why both 0! and 1! equal 1.

1 Like

Your comments are exactly what I was looking for. I came to a similarly messy solution as OP, but I knew there was a better way, I just couldn’t put my finger on it. Thank you!

After doing some reading up on ES6, I managed to refactor my code using reduce and fill():

    // create a new array with num items
    return Array(num).fill()
    // reduce the array starting from 1
     .reduce((prev, curr, index) => prev + prev * index, 1);
}
factorialize(5);```

I believe I have contrived the ugliest way to solve this problem…behold!

   `function factorialize(num) {
        if(num===0){
            return 1;
        }else{
        var array = [1, num];
while (1 < num){
num = num - 1; 
array.push(num);
array.reverse();
console.log(array);
singleNum = array.reduce(function(highNumber, lowNumber){
    return lowNumber * highNumber;
      });

}

return singleNum;
}

}
`

Am I the only one not using an array to solve this problem?
I’m getting the feeling I might not have taken the ‘correct’ route…
This is my code:

function factorialize(num) {
  var answer = 1;
  for (var i = 1; i <= num; i++) {
    answer = i * answer;
  }
  return answer;
}
1 Like

Thanks for the info.
This clears some things up and after I complete the basic JS algorithm challenges, I might go back to see if I can solve it with arrays, for the extra practice.

I made this too. It looks so simple. But works.

this is the route I took. I was initializing num to 1 though because of the wikipedia post on factorials so I was having some trouble at first, came to this post and seen this, almost an exact match. Except I was calling variable answer, X, :smile:

I originally tried solving the problem with recursion (even though we didn’t go over recursion in the lessons) but in the end I went with array.reduce method.

This my solution to this:
if (num===0){
return 1;
}
else{
var singleVal=0;
Var arr = [ ];

function factorialize(num){

for(i=1; i<num+1; i++){
arr.push(i);
singleVal= arr.reduce(function(a,b){return a*b;});
}
return singleVal;

}
}

1 Like

Heres my code. It works for everything except when factorializing 0. i don’t get it.

function factorialize(num) {
var array = [];
var i = 0;

while (i <= num)
array.push(i++);

array.shift();

array = array.reduce(function(a,b){return a*b;});

if (array > 0)

return array;

}

factorialize(5);

This was my solution for this challenge. Hope this helps some people out :slight_smile:

function factorialize(num) {
  for(var i=1,factorial=1;i<=num;i++){
   factorial = factorial*i; 
  }
  return factorial;
}
factorialize(5);

Hmm, no one use a recursive solution?

function factorial(num) {
  if (num <= 1)
    return 1;
  else
    return num * factorial(num - 1);
}