Last active
November 23, 2019 04:23
-
-
Save gregdevs/7dc7c0308a03fc1fdc203bc86628dec5 to your computer and use it in GitHub Desktop.
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 characters
//Probably a Naive solution | |
const sortedArr = [1,2,3,7,9,15,56,76,78]; | |
function findElem(arr, n){ | |
const firstHalf = arr.slice(0, arr.length / 2), | |
secondHalf = arr.slice(arr.length / 2); | |
let arrToPass; | |
if (arr.length === 1 && arr[0] !== n){ | |
return false | |
} | |
else{ | |
if (arr[0] === n){ | |
return true | |
} | |
if (secondHalf[0] > n ){ | |
arrToPass = firstHalf; | |
} | |
else { | |
arrToPass = secondHalf; | |
} | |
return findElem(arrToPass, n) | |
} | |
} | |
console.log(findElem(sortedArr, 15)) // => true | |
console.log(findElem(sortedArr, 78)) // => true | |
console.log(findElem(sortedArr, 9)) // => true | |
console.log(findElem(sortedArr, 89)) // => false | |
console.log(findElem(sortedArr, 111)) // => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment