Factorialize a Number working but cannot complete the challenge! why?

Factorialize a Number working but cannot complete the challenge! why?
0.0 0

#1

Tell us what’s happening:
Its working and checks all the requirements so far but I cannot get past the challenge. I will try something else but whats wrong with this?

Your code so far


var nums = [];
var result = 0;
function factorialize(num) {
  for (var i = 1; i < num  + 1; i++) {
     nums.push(i);
  }
  if (num > 1) {
  result = nums.reduce(function(a, b) {
    return a * b;
  }, 1);
  return result; } else {
  return 1;
} }

factorialize(20);
**Your browser information:**

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

**Link to the challenge:**
https://www.freecodecamp.org/challenges/factorialize-a-number
[details="Summary"]
This text will be hidden
[/details]

#2

I get the error “ReferenceError: nums is not defined”. You have a variable that is not defined anywhere.


#3

When I declare that, it works fine.

Also, I’d be careful of global variables, like you did with result. It’s not a factor here, but when fcc runs the tests sometimes it will accumulate results. Again, not an issue here since you’re using an assignment operator, but it could cause a problem on other challenges and it is bad habit to have variables in more scope than they need.

Also, I’d work on properly tabbing/organizing your code. It’s a good habit that makes coding easier in the long run.


#4

thanks a lot. I declared nums array before result but somehow It’s not here! I wasn’t aware of recursion and now I am trying to understand how recursion works to get past the challenge.