I would do something like this : here is jsbin link :
https://jsbin.com/lijitet/8/edit?js,console
/**
* Linear Search : Recursion
* Returns index if found -1 otherwise
* Procedure LinearSearch(Array A, int x, int i)
* n = A.length and i is starting position of array
* if (A[i] === x) return i
* if (i > n) LinearSearch(A, x, i++)
*
*/
function LinearSearchRecursively(ArrayGiven, x, i) {
const arrayLength = ArrayGiven.length;
if (i > (arrayLength - 1)) {
return -1;
}
if(ArrayGiven[i] === x) {
return i;
}
return LinearSearchRecursively(ArrayGiven, x, i+1);
}
// write your test cases here :
const testArray = [ 1, 2, 3, 4, 5, 6, 7];
console.log(`Missing Element : ${LinearSearchRecursively(testArray, 7, 0)}`);
Please feel free to add. thanks.