The code is in javascript. The goal of the highest function is to return the largest number in the array g. I want this to be recursive and no use of while loop or for loop. So the return value should be 732. /// edit...i know that doing sort then pop the last item is the best approach but i was just practicing recursive function and too lazy to do sort. i understand quicksort and mergesort. at the time, it was 200am in the morning so i was tire.
function highest(arr, index, largest)
{
var largest = largest || arr[0];
if(index < arr.length)
{
if(arr[index] > arr[0])
{
largest = arr[index];
return highest(arr, index + 1, largest)
}
else
{
return highest(arr,index + 1, largest)
}
}
return largest;
}
var g = [2,22,332,4,5,6,732,3,2,3];
console.log(highest(g, 0))