Skip to content

Instantly share code, notes, and snippets.

@CarlMungazi
Last active July 23, 2017 22:41
Show Gist options
  • Save CarlMungazi/6a4e034d7c15ed2be889ae499d641655 to your computer and use it in GitHub Desktop.
Save CarlMungazi/6a4e034d7c15ed2be889ae499d641655 to your computer and use it in GitHub Desktop.
Factorialize a Number

Task: Return the factorial of the provided integer.

Pseudocode:

  • Get the integer value (n) as a parameter
    • If the the number is equal to 0
      • return 1
    • return the value of n multiplied by result of n - 1

Actual Code:

function factorialize(num) {
  // base case - if num is equal to 0, stop the recursion
  if (num === 0) {
    return 1;
  }
  
  // recursive case - will run except when num is equal to 0
  return num * factorialize(num - 1);
}

factorialize(3); // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment