src/helpers/util.js
/*
Find the closest value in a sorted array
*/
Find the closest value in a sorted array
Find the closest value in a sorted array
function findClosest(arrSorted, value) {
  let closest = arrSorted[0]
  let closestDiff = Math.abs(arrSorted[0] - value)
  for (let i = 1; i < arrSorted.length; i++) {
    let diff = Math.abs(arrSorted[i] - value)
    if (diff < closestDiff) {
      closestDiff = diff
      closest = arrSorted[i]
    } else {
      return closest
    }
  }
  return arrSorted[arrSorted.length - 1]
}
function findClosest ( arrSorted , value ) { let closest = arrSorted [ 0 ] let closestDiff = Math . abs ( arrSorted [ 0 ] - value ) for ( let i = 1 ; i < arrSorted . length ; i ++ ) { let diff = Math . abs ( arrSorted [ i ] - value ) if ( diff < closestDiff ) { closestDiff = diff closest = arrSorted [ i ] } else { return closest } } return arrSorted [ arrSorted . length - 1 ] }
Find the closest value in a sorted array
Find the closest value in a sorted array