Basic Algorithm Scripting: Factorialize a Number - Console.log issue

Tell us what’s happening:
Why does console.log(a = a*i) impact the value of a?

I know that i am setting a*i equal to a. My understanding is that console.log outputs results however it does not impact the variable. My understanding is most likely incorrect.

I also know that the issue can be resolved by removing the “a=” and using console.log(a*i) instead. I wanted to get a better understanding of how the console.log works. Any help you be appreciated.

Your code so far


function factorialize(num) {
var a = 1;
for(var i=1;i<=num;i++){
 console.log(a = a*i);
  a = a*i;
}
return a;
}

factorialize(5);

Challenge: Factorialize a Number

Link to the challenge:

Welcome, hasfcc.

A quick way to help you understand this behaviour is for you to:

  1. Remove the log inside the function
  2. Add a return statement to the a = ...
  3. Replace your last line with this:
console.log(factorialize(5));

Hope this helps

the expression inside the parenthesis is evaluated, and then the generated value is printed to the console. If you have an assignment operator, the assignment is executed, so, even if it is inside the console.log, it does what it usually does, which is assign a new value to the a variable