Source

math/getClosestNumber.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Get the number of an array of numbers closest to a passed number
 *
 * When there is a condition that,
 * two number have same difference to 'num',
 * the previous one will be returned.
 *
 * Reference: https://stackoverflow.com/questions/8584902/get-closest-number-out-of-array
 *
 * @param num The passed number
 * @param numbers The array of numbers
 * @category math
 * @module getClosestNumber
 */
var getClosestNumber = function (num, numbers) { return numbers
    .reduce(function (prev, curr) { return (Math.abs(curr - num) < Math.abs(prev - num) ? curr : prev); }); };
exports.default = getClosestNumber;