Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

JS Recursive getLength of Array

function getLength(array) {
    return 0 in array ? 1 + getLength(array.slice(1)) : 0;
}

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
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #JS #Recursive #getLength #Array
ADD COMMENT
Topic
Name
5+8 =