UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

28 lines (27 loc) 905 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SearchStrings = SearchStrings; function SearchStrings(a, x) { // discuss at: https://locutus.io/golang/sort/SearchStrings // parity verified: Go 1.23 // original by: Kevin van Zonneveld (https://kvz.io) // example 1: SearchStrings(['a', 'c', 'e', 'g'], 'e') // returns 1: 2 // example 2: SearchStrings(['a', 'c', 'e', 'g'], 'd') // returns 2: 2 // example 3: SearchStrings(['a', 'c', 'e', 'g'], 'z') // returns 3: 4 let low = 0; let high = a.length; while (low < high) { const mid = low + Math.floor((high - low) / 2); const candidate = a[mid]; if (candidate !== undefined && candidate < x) { low = mid + 1; } else { high = mid; } } return low; }