Learning about function in javascript and i don't understand how the function is returning 720 as an answer to the operation performed in it.
function factorial(num) {
if(num <= 0) {
return 1;
}else {
return (num * factorial(num-1) )
}
}
console.log(factorial(6)) // logs 720. why?
Here is the link to the tutorial on tutorialspoint, under recurring function I've tried working it on the firefox console, but still can't figure why it 720.
this is what i think should be logged
function factorial(num) {
if(num <= 0) {
return 1; // won't return, because 6 is greater and not equal to 0
}else {
return (num * factorial(num-1) ) // bracket first(6-1),
//then multiply by 6 (6 * 5 = 30)
}
}
console.log(factorial(6)) // 30
please help, obviously I'm the one making the error!
Forgive me if this is a rookie way of asking a question on this site, i had to create this account in the hope of getting an answer, thanks.