How to round a number?

Hi all. Can you guys tell me how to solve the problem.
I, for example, there is an array of numbers:
const arrayNumbers = [-5, 0, 5, 10, 15, 20, 25, 30, 35]
const n = 23.45
const res = null

My task in the following, 23.45, closest to the number 25 (from the array) in the variable res I want to record this number 25.
If you come, for example, 11,38, the variable res will need to record the number 10 (the number of the array).
April 7th 20 at 15:31
5 answers
April 7th 20 at 15:33
const arrayNumbers = [-5, 0, 5, 10, 15, 20, 25, 30, 35]
const n = 23.45
const bestMatch = arrayNumbers.reduce(searchClosestNum, {delta: Infinity, value: null})
const res = bestMatch.value
console.log(bestMatch)

searchClosestNum function(acc, val) {
 const delta = Math.abs(val - n)
 if (delta < acc.delta) {
 acc.delta = delta
 acc.value = val
}
 return acc
}

April 7th 20 at 15:35
First, `res` you have a constant, and, after the Declaration, record nothing happens. After elimination of cant start writing algorithm, it is just like a cork. Go through each element of the array and subtracted it from your number, the answer is that number the result of the subtraction of which will be the smallest. To circumvent the array using `for` operator or method `forEach`, values are compared with the `if`operator.
https://developer.mozilla.org/ru/docs/Web/JavaScri...
https://developer.mozilla.org/ru/docs/Web/JavaScri...
https://developer.mozilla.org/ru/docs/Web/JavaScri...
Fu to do that! The array is sorted, you need search half of the division. - Alva.Botsfo commented on April 7th 20 at 15:38
@claire, govnokod on. - ima_Mills78 commented on April 7th 20 at 15:41
April 7th 20 at 15:37
As I understand, the array is sorted. Hence, it is necessary to check whether the initial number in the range of "from lesser to the greater element of the array". If included, then look for the method of half division (Donald Knuth "Sorting and searching"; or Niklaus Wirth "Algorithms and data structures").

Finding - check/compare the element is found and the previous/next (depending on how you look will). Choose which one You like best.

And can be stupid to iterate through all the array elements and choose the best.
April 7th 20 at 15:39
?
const arrayNumbers = [-5, 0, 5, 10, 15, 20, 25, 30, 35]
const n = 23.45
const buf = arrayNumbers.map(number => { return Math.abs(n number); });
const res = arrayNumbers[buf.indexOf(Math.min(...buf))];

console.log(res);
Not very efficient algorithm
1. map - overkill
2. indexOf is another bust
3. Math.min - another bust
4. spend operation - another bust. - sven42 commented on April 7th 20 at 15:42
April 7th 20 at 15:41
I agree, great guys, but the guy is the problem a simple algorithm, and some of you give the solution with map, reduce, spread half of the day he will still have to figure out how it works. Can simply need for starters? Waiting for criticism.

const neigborMin = (n, arr) => {
 let min = n
 let index = 0
 for (let i = 0; i < arr.length; i++) {
 if (Math.abs(n - arr[i]) <= min) {
 min = Math.abs(n - arr[i])
 index = i
}
}
 return arr[index]
}
const array = [-5, 0, 5, 10, 15, 20, 25, 30, 35]
let res = null
res = neigborMin(11.38, array)
console.log('result 1: ', res)
res = neigborMin(23.45, array)
console.log('result 2: ', res)
res = neigborMin(0, array)
console.log('result 3: ', res)
res = neigborMin(-5.10, array)
console.log('result 4: ', res)
res = neigborMin(5.10, array)
console.log('result 5: ', res)

Find more questions by tags JavaScript