Created
October 4, 2020 09:04
-
-
Save muhsalaa/40056193da9796297492443fdd3cfdd1 to your computer and use it in GitHub Desktop.
This file contains 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 characters
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