Hello guys, I’m doing the factorial exercise in this linkJavaScript Practice: Arrays, Loops, Objects, Iterators
If I use ‘return’ inside the ‘for loop’, the output is 30. See below
const factorial = num => {
for (i = num -1; i >= 1; i--) {
return num *= i
}
}
console.log(factorial(6))
//output 30
If I use ‘return’ after the ‘for loop’, the output is 720 which is the correct answer. See below:
// Write function below
const factorial = num => {
for (i = num -1; i >= 1; i--) {
num *= i
}
return num
}
console.log(factorial(6))
//output 720
Why is that?