Why won't this factorialize function work?

Why won't this factorialize function work?
0.0 0

#1

Tell us what’s happening:

Hi ! So I’m trying to solve the factorialize challenge, and I got the code written below.
But it doesn’t work at all, it says "unexpected token ) ".
I don’t know why it wouldn’t work, it’s a longer version than the 2nd solution in the hint thread ( I can’t understand how recursion works. I kinda do, but it’s not fully understood and kind of hard for me to process, I’ll probably get it with time ), and I used i++ instead of i–, and began at i = 1, but it’s still supposed to run properly, right ? I tried to write things down and I fixed a few mistakes, now I don’t see any and don’t see what the problem is.

What’s the issue ?

Thank you !

Your code so far

function factorialize(num) {
  var arrayNum = ();
  
  if(num == 0) {      
    return 1;   
  } else {
    var newArray = ();
    var total = 1;
    for (var i = 1; i <= num; i++) {
      newArray.push(i);

      var total = newArray[i - 1] * total;
      
      num = total;
    }      
  }
  return num;
}

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/65.0.3325.181 Safari/537.36.

Link to the challenge:


#2

First of all, arrays look like [] in JS, not (). So var newArray = (); and var arrayNum = (); are syntax errors.

Second, you are changing num - don’t do this, there is no need even if you get the logic right. But you start the loop at 1. Then you push 1 into the array. Then you set num to 1. Then the loop finishes because 1 <= 1, so newArray always ends up being [1], and the end result is always 1

Really, really try not to manipulate input values while you’re looping over them, you will get yourself in all kinds of mess, it makes it extremely hard for you to keep the state in your head while you try to figure out what is happening.

Here is the code in environment where you can walk through it step by step


#3

Thanks a lot for your answer ! I fixed the syntax and the looping mistake and it works. It’s way more complicated than the Solution 2, but it works.

What do you mean when you say that I should not manipulate input values when looping over them ? Do you mean that I shouldn’t write things like " total = newArray[i - 1] * total; " ?
If so, then why ?

And thanks for the amazing tool too, I’ll bookmark it !


#4

You are changing the value of the argument num in the loop. You are also changing the value of a variable total which is updated by multiplying by a the last value you have added to an array. So when you try to debug it, you need to keep those changing values in your head as it changes over time, which is almost impossible. This is a very simple example, but even in a program this simple it makes debugging extremely difficult.

Note that you’re dealing with numbers: you can just multiply them directly, you don’t need to use an array or anything.

function factorialize(num) {  
  if(num == 0) return 1;   
  
  var total = 1;
  for (var i = 1; i <= num; i++) {
      var total = total * i;
  }
  return total;
}

#5

Oh okay yes I see. Thanks for your help !