Created
          August 30, 2015 07:04 
        
      - 
      
 - 
        
Save Sigmus/1dbf58b2e73a0a66417a to your computer and use it in GitHub Desktop.  
Revisions
- 
        
Sigmus created this gist
Aug 30, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ --- layout: post title: Next circular array item --- A function that returns the next item in a given array. Starts over when the last item is reached. ```javascript var next = nextCircularItem(['x', 'y', 'z']); next(); // x next(); // y next(); // z next(); // x // etc function nextCircularItem(arr) { var len = arr.length; var current = -1; return function() { var next = current + 1; current = next === len ? 0 : next; return arr[current]; }; } ```