DEV Community

Discussion on: Challenge: Get Closest Number in an Array

Collapse
 
andreyvladikin profile image
Andrey Vladikin

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