Skip to content

Instantly share code, notes, and snippets.

@muhsalaa
Created October 4, 2020 09:04
Show Gist options
  • Save muhsalaa/40056193da9796297492443fdd3cfdd1 to your computer and use it in GitHub Desktop.
Save muhsalaa/40056193da9796297492443fdd3cfdd1 to your computer and use it in GitHub Desktop.
function isExist(target, array) {
let sorted = array.sort((a, b) => a - b);
let midPoint = Math.floor(sorted.length / 2);
if (sorted[midPoint] === target) {
console.log('exist');
} else if (sorted.length <= 1) {
console.log('not exist');
} else if (sorted[midPoint] < target) {
isExist(target, sorted.slice(midPoint, sorted.length));
} else if (sorted[midPoint] > target) {
isExist(target, sorted.slice(0, midPoint));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment