Skip to content

Instantly share code, notes, and snippets.

@dshaw
Created September 5, 2010 04:06
Show Gist options
  • Select an option

  • Save dshaw/565744 to your computer and use it in GitHub Desktop.

Select an option

Save dshaw/565744 to your computer and use it in GitHub Desktop.
recursive factorial function
function factorial( n ) {
if (n > 1) {
return factorial(n-1) * n;
} else {
return 1;
}
}
console.log( factorial( 1 ) 1 );
console.log( factorial( 3 ), 6 );
console.log( factorial( 5 ), 120 );
console.log( factorial( 6 ), 720 );
console.log( factorial( 9 ), 362880 );
console.log( factorial( 15 ), 1307674368000 );
console.log( factorial( 20 ), 2432902008176640000 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment