Skip to content

Instantly share code, notes, and snippets.

@trxcllnt
Last active October 10, 2016 03:53
Show Gist options
  • Save trxcllnt/4fc7f87f3f141ea225492ea760f00a34 to your computer and use it in GitHub Desktop.
Save trxcllnt/4fc7f87f3f141ea225492ea760f00a34 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, 260, 500, 515, 750, 760, 1005];
const output = [ -5, 240, 250, 15, 0, -240, -250, 0, 250, 240, 0, -15, -250, -240, 5];
let runs = -1;
function adjust(value, bounds) {
if (isNaN(value = Number(value))) {
throw new Error('NaN value');
}
const sep = Math.round(value / bounds);
const pos = sep % 2 === 0 ? 1 : -1;
return (value - (bounds * sep)) * pos;
}
console.clear();
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 bounds = 500;
var inputs = [-1005, -760, -750, -515, -500, -260, -250, 0, 250, 260, 500, 515, 750, 760, 1005];
var output = [-5, 240, 250, 15, 0, -240, -250, 0, 250, 240, 0, -15, -250, -240, 5];
var runs = -1;
function adjust(value, bounds) {
if (isNaN(value = Number(value))) {
throw new Error('NaN value');
}
var sep = Math.round(value / bounds);
var pos = sep % 2 === 0 ? 1 : -1;
return (value - bounds * sep) * pos;
}
console.clear();
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