0

For example, here is an array like [100, 200, 300, 400, 500]
what I want is

input: 110 output:100
input: 290 output:200
input: 450 output:400
input: 999 output:500

...

Is there a smart way to do it?

chii
  • 2,111
  • 3
  • 17
  • 21
  • you loop through your sorted array and u stop when the number ahead is greater(meaning the current number is "most nearest small number") – The Bomb Squad Oct 19 '22 at 02:59
  • Since you tagged lodash, the [sortedIndex](https://lodash.com/docs/4.17.15#sortedIndex) function (minus 1) might be what you want – Ricky Mo Oct 19 '22 at 03:10
  • @RickyMo thank you, the lodash way is very smart! – chii Oct 19 '22 at 03:15

0 Answers0