There are 2 changes to be made:
1. updation conditions : i=k+1 and j=k-1 //reason can be found in other ans
2.while loop condition : while (Y[k] != x) && (i < = j) //equal to added here , other ans missed this
let a[]={1,2,3,4,5,6,7,8,9,10};
Without 2nd condition f(a,10) will fail