Solution Review: The Factorial of a Number (Loops)
This lesson explains the solution for the factorial of a number exercise.
We'll cover the following...
Solution
Press + to interact
let fact = (n: int) => {/* Check if n is 0 or 1 */if (n == 0 || n == 1) {1;}else {let i = ref(n);let prod = ref(1); /* Give the product a default value of 1 */while (i^ > 1) {prod := prod^ * i^; /* Multiple prod with i in each iteration */i := i^ - 1; /* Decrement i until it reaches 1 */};prod^;};};Js.log(fact(3));
...