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);
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