brain-game
Version:
39 lines (27 loc) • 1.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _ = require('../');
var _2 = _interopRequireDefault(_);
var _utils = require('../utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const description = 'Balance the given number.\n';
const generate = () => (0, _utils.getRandomInt)(100, 1000);
const isBalansed = numArray => numArray[numArray.length - 1] - numArray[0] <= 1;
const smooth = arr => {
const smoothedArray = arr.slice();
smoothedArray[0] = arr[0] + 1;
smoothedArray[smoothedArray.length - 1] = arr[arr.length - 1] - 1;
return smoothedArray.sort().slice();
};
const balance = number => {
const sortedArray = (0, _utils.numToSortedArray)(number);
if (isBalansed(sortedArray)) {
return (0, _utils.arrayToInt)(sortedArray);
}
const smoothed = smooth(sortedArray);
return balance((0, _utils.arrayToInt)(smoothed));
};
const toString = number => number.toString();
exports.default = () => (0, _2.default)(description, generate, balance, toString);