Skip to content

Instantly share code, notes, and snippets.

@mw44118
Created May 17, 2011 19:33
Show Gist options
  • Save mw44118/977206 to your computer and use it in GitHub Desktop.
Save mw44118/977206 to your computer and use it in GitHub Desktop.
Cycle through an array
var cyclemaker = function (arr) {
var i=0;
var end=arr.length;
return function () {
var element = arr[i];
if (i == end) {
i = 0;
} else {
i++;
}
if (element === undefined) {
return '';
} else {
return element;
}
};
};
var feelings = ['happy', 'sad', 'bored'];
var f1 = cyclemaker(feelings);
var f2 = cyclemaker(feelings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment