Last active
December 15, 2015 19:28
-
-
Save rickbacci/4a15d678fb47147ed443 to your computer and use it in GitHub Desktop.
Countdown with recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countdown(number) { | |
if (number !== 0) { | |
console.log(number); | |
countdown(number - 1); | |
} else { | |
console.log(0); | |
} | |
} | |
countdown(5); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* factorialGenerator() { | |
var total = 0; | |
var num = 1; | |
while(true) { | |
total += num; | |
num += 1; | |
yield total; | |
} | |
} | |
var factorial = factorialGenerator(); | |
console.log(factorial.next().value); // 1 | |
console.log(factorial.next().value); // 2 | |
console.log(factorial.next().value); // 6 | |
console.log(factorial.next().value); // 10 | |
console.log(factorial.next().value); // 15 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fibonacci(length, sequence) { | |
var fibSequence = sequence || [1, 1]; | |
var fibSequenceLength = fibSequence.length; | |
if (length === 0) { console.log( fibSequence ); | |
} else if (length === 1) { console.log( [1] ); | |
} else if (length === 2) { console.log( fibSequence ); | |
} else { | |
var nextFibNum = | |
fibSequence[fibSequence.length - 2] + fibSequence[fibSequence.length - 1]; | |
fibSequence.push(nextFibNum); | |
fibonacci(length - 1, fibSequence); | |
} | |
} | |
fibonacci(1); | |
fibonacci(2); | |
fibonacci(3); | |
fibonacci(4); | |
fibonacci(10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment