Skip to content

Instantly share code, notes, and snippets.

@willmcneilly
Last active August 29, 2015 13:58
Show Gist options
  • Save willmcneilly/10006033 to your computer and use it in GitHub Desktop.
Save willmcneilly/10006033 to your computer and use it in GitHub Desktop.
Find factorial by iteration, use one return statement in iterator
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Find fctorial by iteration, use one return statement in iterator" />
<meta charset="utf-8">
<title>Find factorial by functional iteration</title>
</head>
<body>
</body>
</html>
// Finding a factorial by iteration. multiply 1 by 2 take the result and multiply by three until we reach n.
function factorial(n) {
return factIter(1, 1, n);
}
function factIter(product, counter, maxCount) {
return counter > maxCount
? product
: factIter(product * counter, counter + 1, maxCount);
}
console.log(factorial(5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment