Skip to content

Instantly share code, notes, and snippets.

@jochri3
Created September 3, 2017 16:29
Show Gist options
  • Save jochri3/b1511c19cc632006bb9e2bda9cd49283 to your computer and use it in GitHub Desktop.
Save jochri3/b1511c19cc632006bb9e2bda9cd49283 to your computer and use it in GitHub Desktop.
null created by ChrisLis - https://repl.it/HaJO/61
// Write a function that takes an integer `n` in; it should return
// n*(n-1)*(n-2)*...*2*1. Assume n >= 0.
//
// As a special case, `factorial(0) == 1`.
//
// Difficulty: easy.
function factorial(n) {
var fact=1
if(n==0)
{
return fact
}
else
{
for(var i=1;i<=n;i++)
{
fact *=i
}
return fact
}
}
// These are tests to check that your code is working. After writing
// your solution, they should all print true.
console.log("\nTests for //factorial")
console.log("===============================================")
console.log(
'factorial(0) == 1: ' + (factorial(0) == 1)
)
console.log(
'factorial(1) == 1: ' + (factorial(1) == 1)
)
console.log(
'factorial(2) == 2: ' + (factorial(2) == 2)
)
console.log(
'factorial(3) == 6: ' + (factorial(3) == 6)
)
console.log(
'factorial(4) == 24: ' + (factorial(4) == 24)
)
console.log("===============================================")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment