Skip to content

Instantly share code, notes, and snippets.

@MowAlon
Last active December 15, 2015 18:25
Show Gist options
  • Save MowAlon/18afb99f5463106f1c86 to your computer and use it in GitHub Desktop.
Save MowAlon/18afb99f5463106f1c86 to your computer and use it in GitHub Desktop.
Recursion and ES6 Generator exercises
function countDown(num) {
console.log(num)
if (num !== 0) {return countDown(--num)}
}
function* factorialGenerator() {
sequence = [1]
while (true) {
yield sequence[sequence.length - 1]
sequence.push((sequence.length + 1) * sequence[sequence.length - 1])
}
}
// 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