Created
February 20, 2024 00:45
-
-
Save tdubs42/5f9233f98393f9b7cbbe9eebc025cd15 to your computer and use it in GitHub Desktop.
Revisions
-
tdubs42 created this gist
Feb 20, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ const binarySearch = (arr, target) => { let left = 0; let right = arr.length; while (right > left) { const indexToCheck = Math.floor((left + right) / 2); const checking = arr[indexToCheck]; console.log(indexToCheck); if (checking === target) { return indexToCheck; } else if (checking < target) { left = indexToCheck + 1; } else { right = indexToCheck; } } return null; } module.exports = binarySearch;