soldair / node-binarysearch

binary search for sorted javascript arrays||array like obejcts. provides method to create sorted index of objects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

binarysearch.closest with decimals

Lee182 opened this issue · comments

const index = binary.closest([25, 33, 50], 32.99)
expect(index).to.eq(1)

// this throws and the index is 0. I would expect it to be 1
// does the compare closest function work with decimals?

closest returns the leading edge. If you want the trailing edge, pass {end: true} to the function.

var bs = require("binarysearch")
const needle = 32.99;
const haystack = [25, 33, 50];

console.log(bs.closest(needle, haystack)); // 0
console.log(bs.closest(needle, haystack, {end: true})); // 1