Stuck on the Factorialize a Number exercise

Stuck on the Factorialize a Number exercise
0

#1

Tell us what’s happening:
Hi, my function returns 0 and I am not quite sure why. Can someone help me understand what I am doing wrong?

Your code so far

function factorialize(num) {
  if (num===0) {
    return 1;
  }
  
  var numbers = [];
  var factorial = 0;
  for (i=num; i>0; i--) {
    numbers.push(num);
    num--;
  }
  
  
  for (i=0; i<numbers.length-1; i++) {
    factorial *= (numbers[i] * numbers[i+1]);
  }
  
  return factorial;
}

factorialize(5);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36.

Link to the challenge:


#2

The following line takes the existing value of factorial and multiplies is by (numbers[i] * numbers[i+1]).

factorial *= (numbers[i] * numbers[i+1]);

You initialized factorial to be zero before getting to this code, so the first and all other iterations of the for loop, factorial will be zero. Think about what number you could initialize factorial to be that would avoid this issue.

You have some other logic issues, but this will get you headed in the right direction.


#3

Thanks @RandellDawson! I was so busy trying to figure out the line you referenced that I completely overlooked the the initial value I set for the factorial value to be.

I solved it now and can finally keep moving :slight_smile: