I have an Array of integers in javascript, [5,10,15,20,25,30,35]
when given a number x, how can I find the element in the array that is closest to that number?
Create a temporary array of the same size as your original array, and populate it with the differences between your x and the array element.
For example, let the temporary array be temp[], and your original array be a[]:
temp[i]=Math.abs(x-a[i]);
Then, return the index of the minimum value in temp[] to the user.