mardi 27 janvier 2015

javascript function output math calculation


I am following a javascript video tutorial on pluralsight.com & they have a javascript function but I don't understand how they got this result:



function factorial(n){
if (n === 0 || n === 1){
return 1;
}
return n * factorial(n-1);

}
console.log( factorial(5) ); // result is 120


Can anyone please explain me why this function is returning 120? I would expect it to return 5 * 4 = 20? >> JS Fiddle Link





Aucun commentaire:

Enregistrer un commentaire