Skip to content

Instantly share code, notes, and snippets.

@faddah
Created November 6, 2013 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faddah/7329054 to your computer and use it in GitHub Desktop.
Save faddah/7329054 to your computer and use it in GitHub Desktop.
step three of code academy recursion #FAIL
// my answer to step three in code academy module on recursion in JavaScript
var stack = [];
function countDown(int) {
stack.push(int);
if (int === 1) {
return 1;
}
return countDown(int - 1);
}
function multiplyEach() {
// Remove the last value of the stack
// and assign it to the variable int
int = stack.pop();
x = stack.length;
// Base case
if (x === 0) {
return 1;
}
// Recursive case
else {
stack[x - 1] = int * stack[x - 1].multiplyEach();
return stack[x - 1];
}
}
// Call the function countDown(7)
countDown(7);
// And then print out the value returned by multiplyEach()
console.log(multiplyEach());
// when i try this, i get error back from console: "TypeError: Object 2 has no method 'multiplyEach'"
// i have no idea what i'm doing wrong here. :(((
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment