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

Sunday, December 9, 2012

Codecademy: Codeyear Iterative recursion


//sample solution


function loopFactorial(n) {
  var result = n;
  while (n > 1) {
    result = result * (n-1);
    n--;
  }
  return result;
}

function recursiveFactorial(n) {
  if (n < 0) {
    return console.log("Must be a positive integer.");
  }
  else if (n === 0) {
    return 1;
  }
  return n * recursiveFactorial(n - 1);
}

var loopResult = loopFactorial(4);// Call the function loopFactorial(n)
var recursiveResult = recursiveFactorial(4);// Call the function recursiveFactorial(n)

console.log("The loop function returned: " + loopResult);
console.log("The recursive function returned: " + recursiveResult);

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts