Factorialize a Number-Using arrays comes out NULL

Factorialize a Number-Using arrays comes out NULL
0

#1

Tell us what’s happening:

I tried making a function that takes a parameter, and puts every number less then the parameter up to zero and pushes it into an array(descending order). and then used a for loop to multiply each index of the array by the next index, but my return comes back null. please help.

Your code so far

function factorialize(num) {
  var arrayOfFactorial = [];
  var factorial = 1;
 for(var i = num;i > 0;i--){
   arrayOfFactorial.push([i]);
   factorial = factorial * arrayOfFactorial[i];
 } 
  
  return factorial;
}

factorialize(5);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36.

Link to the challenge:


#2

I have added a few console.log statements to your solution so you can see what you are attempting to multiply:

function factorialize(num) {
  var arrayOfFactorial = [];
  var factorial = 1;
  for(var i = num; i > 0;i--){
   console.log('i = ' + i);
   
   arrayOfFactorial.push([i]);
   
   console.log('arrayOfFactorial is ' + JSON.stringify(arrayOfFactorial));
   
   console.log('arrayOfFactorial[' + i + '] = ' + arrayOfFactorial[i]);
   factorial = factorial * arrayOfFactorial[i];
   
   console.log('factorial = ' + factorial);
   console.log('');
  } 
  return factorial;
}

factorialize(5);

A few hints:

  1. Look at what num is in the first iteration. Is there an index of num?

  2. You are pushing an array of one element in the following line. Should you push an array or a value in to arrayOfFactorial?

arrayOfFactorial.push([i]);