Skip to content

Instantly share code, notes, and snippets.

@supahfunk
Created October 21, 2021 10:11
Show Gist options
  • Save supahfunk/a91836060c2e34a343f2fce981a8553a to your computer and use it in GitHub Desktop.
Save supahfunk/a91836060c2e34a343f2fce981a8553a to your computer and use it in GitHub Desktop.
Return the closest difference between two points in array
const getClosestDiffInArray = (length, curr, next) => {
const inside = curr > next ? -(curr - next) : next - curr
const outside = curr > next ? length - curr + next : -(length - next + curr)
return Math.abs(inside) < Math.abs(outside) ? inside : outside
}
console.log('return', getClosestDiffInArray(12, 11, 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment