Basic alogorithm scripting

function factorialize(num) {
  let value;
  let newArr = [];
  let factorial;
  for(let i = 1;i<=num;i++){
    //console.log(i);
     factorial = num * (num-i) ;
     console.log(factorial); 
     
  }
  if(num  === 0){
    return 1;
  }
  return factorial;
}

console.log(factorialize(5));

Code so far,I facing problem with the looping of values in the factorial like ,
5! = 5 * 4 * 3 * 2 * 1,which should be standard for all the numbers.

This is your loop
As you are not updating a variable at each iteration, in the last iteration of the loop i is equal to num so you get factorial = num * (num - num)

Your function is returning 0

Try looking at your code with this tool:
http://pythontutor.com/javascript.html

1 Like

Very useful and easier than use your navigator console for beginner.