# JavaScriptWant more resource

#### Veronica

##### Coder
We can use binary search to find the target element in an array. First, we need to sort the array in ascending order. Then, we can use binary search to find the target element in the sorted array. The time complexity of this approach is O(nlogn), where n is the number of elements in the Array. Came across from this Resource.

Can anyone help me with more resources?

Yes most case

try this code above, if you dont want programing into HTML file you can use the JSFiddle OR Codepen its online IDE for development and tests

JavaScript:
``````let recursiveFunction = function (arr, x, start, end) {

// Base Condition
if (start > end) return false;

// Find the middle index
let mid=Math.floor((start + end)/2);

// Compare mid with given key x
if (arr[mid]===x) return true;

// If element at mid is greater than x,
// search in the left half of mid
if(arr[mid] > x)
return recursiveFunction(arr, x, start, mid-1);
else

// If element at mid is smaller than x,
// search in the right half of mid
return recursiveFunction(arr, x, mid+1, end);
}

// Driver code
let arr = [1, 3, 5, 7, 8, 9];
let x = 5;

if (recursiveFunction(arr, x, 0, arr.length-1))

x = 6;

if (recursiveFunction(arr, x, 0, arr.length-1))
</script>``````

Here binary

JavaScript:
``````var a = [
1,
2,
4,
6,
1,
100,
0,
10000,
3
];

a.sort(function (a, b) {
return a - b;
});

function binarySearch(arr, i) {
var mid = Math.floor(arr.length / 2);

if (arr[mid] === i) {
return arr[mid];
} else if (arr[mid] < i && arr.length > 1) {
return binarySearch(arr.splice(mid, Number.MAX_VALUE), i);
} else if (arr[mid] > i && arr.length > 1) {
return binarySearch(arr.splice(0, mid), i);
} else {
return -1;
}

}
var result = binarySearch(a, 100);