Skip to content

Instantly share code, notes, and snippets.

@oscarkuo
Created February 16, 2016 09:35
Show Gist options
  • Save oscarkuo/c2cf4994563a9f4989f4 to your computer and use it in GitHub Desktop.
Save oscarkuo/c2cf4994563a9f4989f4 to your computer and use it in GitHub Desktop.
const _ = require('lodash');
const assert = require('assert');
const rotate = (value, max, min) => {
const distance = max - min + 1;
if (value > max) {
return min + ((value - max - 1) % distance);
} else if (value < min) {
return max - ((min - value - 1) % distance);
} else {
return value;
}
}
const test = (expect, value, max, min) => {
assert.equal(expect, rotate(value, max, min), `${value} should be ${expect} for range ${min}-${max}`);
}
test(20, 20, 21, 19);
test(21, 18, 21, 19);
test(19, 22, 21, 19);
test(21, 24, 21, 19);
test(19, 16, 21, 19)
test(19, 25, 21, 19);
test(21, 15, 21, 19);
test(24, 19, 25, 21);
test(25, 20, 25, 21);
test(23, 18, 25, 21);
test(23, 28, 25, 21);
test(24, 29, 25, 21);
test(21, 31, 25, 21);
test(4, -1, 4, 0);
test(3, -2, 4, 0);
test(2, -3, 4, 0);
test(1, -4, 4, 0);
test(0, -5, 4, 0);
test(4, -6, 4, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment