This is a job for Reduce!
const nums = [100, 200, 400, 800, 1600, 3200, 6400, 128000]
const given_num = 900
const closestReducer = g => (a, b) =>
Math.abs(g - a) < Math.abs(g - b) ? a : b
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.