Get the length of an array using recursion without accessing its length property.
My Code Below:
function getLength(array, count=0) {
if (array.length === 0){
return 0
}
else {
count ++;
array.pop()
return getLength(array, count)
}
return count;
}
// To check if you've completed the challenge, uncomment these console.logs!
console.log(getLength([1])); // -> 1
console.log(getLength([1, 2])); // -> 2
console.log(getLength([1, 2, 3, 4, 5])); // -> 5
console.log(getLength([], 0)); // -> 0
When I run my code on the third console.log:
console.log(getLength([1, 2]));
it returns 0 instead of 2
What am I doing wrong?
///////////////////////////////////////////////////////////////////////////
figured it out:
function getLength(array, count=0) {
if (array.length === 0){
return count
}
else {
count ++;
array.pop()
return getLength(array, count)
}
}
return 0.length.