Skip to content

Instantly share code, notes, and snippets.

@KMR-zoar
Last active April 12, 2019 08:15
Show Gist options
  • Save KMR-zoar/c4a271a0e2e5d68f930b3e1b86903435 to your computer and use it in GitHub Desktop.
Save KMR-zoar/c4a271a0e2e5d68f930b3e1b86903435 to your computer and use it in GitHub Desktop.
指定された範囲を段階に分けて配列の要素を返す関数
const colorStep = (inputInt, maxInt, minInt, colorPalette) => {
const dLength = colorPalette.length - 1;
const isBetween = inputInt > maxInt || inputInt < minInt ? false : true;
if (isBetween) {
const d = maxInt - minInt;
const dInt = inputInt - minInt;
const colorIndex = Math.floor(dInt / (d / dLength));
return colorPalette[colorIndex];
} else {
return undefined;
}
};
module.exports = colorStep;
const assert = require('assert');
const colorStep = require('colorStep.js');
const colorPalette = ['0', '1', '2', '3', '4', '5', '6', '7'];
const maxInt = 1500;
const minInt = 1400;
describe(
'範囲内の入力値に対するレスポンス' + minInt + '以上' + maxInt + '以下',
() => {
it('1432 -> 2', () => {
assert.equal(
colorPalette[2],
colorStep(1432, maxInt, minInt, colorPalette)
);
});
it('1455 -> 3', () => {
assert.equal(
colorPalette[3],
colorStep(1455, maxInt, minInt, colorPalette)
);
});
it('1470 -> 4', () => {
assert.equal(
colorPalette[4],
colorStep(1470, maxInt, minInt, colorPalette)
);
});
it('1492 -> 6', () => {
assert.equal(
colorPalette[6],
colorStep(1492, maxInt, minInt, colorPalette)
);
});
it('1413 -> 0', () => {
assert.equal(
colorPalette[0],
colorStep(1413, maxInt, minInt, colorPalette)
);
});
it('1400 -> 0', () => {
assert.equal(
colorPalette[0],
colorStep(1400, maxInt, minInt, colorPalette)
);
});
it('1500 -> 7', () => {
assert.equal(
colorPalette[7],
colorStep(1500, maxInt, minInt, colorPalette)
);
});
}
);
describe('範囲外の値に対するレスポンス', () => {
it('1399 -> undefined', () => {
assert.equal(undefined, colorStep(1399, maxInt, minInt, colorPalette));
});
it('1501 -> undefined', () => {
assert.equal(undefined, colorStep(1501, maxInt, minInt, colorPalette));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment