Skip to content

Instantly share code, notes, and snippets.

@electerious
Last active January 24, 2017 09:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electerious/14fa5890df0c9bb6d3e5 to your computer and use it in GitHub Desktop.
Save electerious/14fa5890df0c9bb6d3e5 to your computer and use it in GitHub Desktop.
Count up and down between two numbers
const counter = (min, max, initial) => {
let index = initial - min
let length = max - min + 1
return (modifier = 0) => {
index = (index + modifier) % length
if (index>=0) index = 0 + index
if (index<0) index = length + index
return min + index
}
}
@electerious
Copy link
Author

electerious commented Nov 13, 2015

Example:

let c = counter(2, 5, 5)

c(1) // 2
c(1) // 3
c(3) // 2
c(-2) // 4
c(0) // 4
c() // 4

@electerious
Copy link
Author

Moved to count-between.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment