Quotes help make search much faster. Example: "Practice Makes Perfect"

Sunday, December 9, 2012

Codecademy: Codeyear Termination Condition


//sample solution


function factorial(n) {
  if (n < 0) {
    // Termination condition to prevent infinite recursion
    console.log("negative numbers are not allowed");
    return;
  }
  // Base case
  if (n === 0) {
    return 1;
  }
  // Recursive case
  return n * factorial(n -1);
}
factorial(-5);
factorial(-1);
factorial(5);

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts