re: Challenge: Get Closest Number in an Array VIEW POST

VIEW FULL DISCUSSION
 

This is a job for Reduce!

JavaScript:

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

nums.reduce(closestReducer(given_num))
//=> 800
code of conduct - report abuse