Skip to content

Instantly share code, notes, and snippets.

@trxcllnt
Created October 10, 2016 03:28
Show Gist options
  • Save trxcllnt/160d7540bfab3517eb9a44f83ab2c620 to your computer and use it in GitHub Desktop.
Save trxcllnt/160d7540bfab3517eb9a44f83ab2c620 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<!-- put markup and other contents here -->
</body>
</html>
const bounds = 500;
const inputs = [-1005, -760, -750, -515, -500, -260, -250, 0, 250, 500, 750, 760, 1005];
const output = [ -5, 240, 250, 15, 0, -240, -250, 0, 250, 0, 250, -240, 5];
// const output = [-3, 0, 3, 0, 3, 0, -3, 0, 3, 0, -3];
// const inputs = [15, 12, 9, 6, 3, 0, -3, -6, -9, 12, -15];
let runs = -1;
function adjust(value, bounds) {
if (isNaN(value)) {
throw new Error('NaN value');
} else if (value === Infinity) {
throw new Error('Infinity value');
} else if (++runs > 10) {
throw new Error('runs > 10');
}
if (Math.abs(value) <= bounds * 0.5) {
return value % bounds;
} else if (Math.abs(value) < bounds) {
return (bounds % value) * Math.sign(value);
} else if (Math.abs(value) < bounds * 1.5) {
return (value % bounds);
} else {
return adjust(bounds - (value % bounds), bounds);
}
}
inputs.forEach((value, index) => {
runs = -1;
const actual = adjust(value, bounds);
const expected = output[index];
if (actual !== expected) {
throw new Error(`${value}: expected ${actual} to equal ${expected}`);
} else {
console.log(`${value}: success`);
}
});
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"babel-runtime": "6.11.6"
}
}
'use strict';
var _sign = require('babel-runtime/core-js/math/sign');
var _sign2 = _interopRequireDefault(_sign);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var bounds = 500;
var inputs = [-1005, -760, -750, -515, -500, -260, -250, 0, 250, 500, 750, 760, 1005];
var output = [-5, 240, 250, 15, 0, -240, -250, 0, 250, 0, 250, -240, 5];
// const output = [-3, 0, 3, 0, 3, 0, -3, 0, 3, 0, -3];
// const inputs = [15, 12, 9, 6, 3, 0, -3, -6, -9, 12, -15];
var runs = -1;
function adjust(value, bounds) {
if (isNaN(value)) {
throw new Error('NaN value');
} else if (value === Infinity) {
throw new Error('Infinity value');
} else if (++runs > 10) {
throw new Error('runs > 10');
}
if (Math.abs(value) <= bounds * 0.5) {
return value % bounds;
} else if (Math.abs(value) < bounds) {
return bounds % value * (0, _sign2.default)(value);
} else if (Math.abs(value) < bounds * 1.5) {
return value % bounds;
} else {
return adjust(bounds - value % bounds, bounds);
}
}
inputs.forEach(function (value, index) {
runs = -1;
var actual = adjust(value, bounds);
var expected = output[index];
if (actual !== expected) {
throw new Error(value + ': expected ' + actual + ' to equal ' + expected);
} else {
console.log(value + ': success');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment