I'm trying to select Nth element from array using this function:
function nthArr(arr, index){
if (index === 0)
return arr[index];
else nthArr(arr.slice(1), --index);
}
nthArr([1,2,3,4,5,6],3)
I would await that it returns 4, but instead I get 'undefined'.
How should I return correct value?
returnsomewhere afterelse?arr[index]work for all indices anyway?