We're a place where coders share, stay up-to-date and grow their careers.
JS:
const nums = [100, 200, 400, 800, 1600, 3200, 6400, 128000]; const givenNum = 900;
nums .map(n => ({n, d: Math.abs(n-givenNum)})) .sort((n1, n2) => Math.sign(n1.d - n2.d))[0].n
JS:
const nums = [100, 200, 400, 800, 1600, 3200, 6400, 128000];
const givenNum = 900;
nums
.map(n => ({n, d: Math.abs(n-givenNum)}))
.sort((n1, n2) => Math.sign(n1.d - n2.d))[0].n