Last active
December 15, 2015 18:25
-
-
Save MowAlon/18afb99f5463106f1c86 to your computer and use it in GitHub Desktop.
Recursion and ES6 Generator exercises
This file contains 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(num) { | |
console.log(num) | |
if (num !== 0) {return countDown(--num)} | |
} |
This file contains 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() { | |
sequence = [1] | |
while (true) { | |
yield sequence[sequence.length - 1] | |
sequence.push((sequence.length + 1) * sequence[sequence.length - 1]) | |
} | |
} |
This file contains 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
// I didn't actually figure this out :( | |
// I had to look it up and modify it to meet the spec (don't start with zero) | |
// ... but it helped me better understand things so I could make the fibonacci generator later | |
var fibonacci_series = function (n) | |
{ | |
if (n===1) | |
{ | |
return [0, 1] | |
} | |
else | |
{ | |
var s = fibonacci_series(n - 1) | |
s.push(s[s.length - 1] + s[s.length - 2]) | |
if (s[0] === 0) {s.shift()} | |
return s | |
} | |
} | |
// And I did this one on my own | |
function* fibonacci() { | |
var sequence = [1,1] | |
while (true) { | |
sequence.push(sequence[sequence.length - 2] + sequence[sequence.length - 1]) | |
yield sequence | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment