Skip to content

Instantly share code, notes, and snippets.

@myfashionhub
Last active August 29, 2015 14:05
Show Gist options
  • Save myfashionhub/f9253f5b839168692ae2 to your computer and use it in GitHub Desktop.
Save myfashionhub/f9253f5b839168692ae2 to your computer and use it in GitHub Desktop.
Sequence generator (Javascript exercise)
var naturals = new NaturalGen();
var evens = new EvenGen();
var odds = new OddGen();
var fibbs = new FibbGen();
var matches = new Match(evens, fibbs);
for (var i = 0; i < 10; i++) {
console.log(naturals.next());
console.log(evens.next());
console.log(odds.next());
console.log(fibbs.next());
console.log(matches.next());
}
// Should return: 1 2 3 4 5
function NaturalGen() {
var n = 1;
this.next = function() {
return n++;
}
}
// 2 4 6 8 ... 20
function EvenGen() {
var n = 1;
this.next = function() {
return ;
}
}
// 1 3 5 7 ... 19
function OddGen() {
this.next = function() {
return ;
}
}
function FibbGen() {
var n = 0;
var m = 1;
this.next = function() {
var p = ;
return ;
}
}
// Return similar numbers from 2 sequels
function Match(seqA, seqB) {
this.next = function() {
var a = seqA.next();
var b = seqB.next();
while (a != b) {
}
return a;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment