// solution 1 : via iteration/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/varsearch=function (nums,target){lethigh=nums.length-1;letlow=0;while (low<=high){letmid=parseInt((low+high)/2);if (nums[mid]===target){returnmid;}elseif (nums[mid]>target){high=mid-1;}else{low=mid+1;}}return-1;};// Solution 2 : via recursionconstsearchRecursive=function (nums,target){letn=nums.length;constbinarySearch=function (nums,low,high,target){if (high>=low){letmid=parseInt((low+high)/2);// If the element is present at the middle// itselfif (nums[mid]==target)returnmid;// If element is smaller than mid, then// it can only be present in left subarrayif (nums[mid]>target)returnbinarySearch(nums,low,mid-1,target);// Else the element can only be present// in right subarrayreturnbinarySearch(nums,mid+1,high,target);}// We reach here when element is not// present in arrayreturn-1;};returnbinarySearch(nums,0,n-1,target);};searchRecursive([-1,0,3,5,9,12],9);