Last active
June 16, 2016 00:46
-
-
Save jafow/6402734e3daf1056ce80d56825c59cf7 to your computer and use it in GitHub Desktop.
getLength
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Write a fn `getLength` that takes one argument, an array, and returns the length of that array. | |
// getLength should be written without using the built-in .length() method, and without any `for` or `while` loops. | |
const getLength = ([first, ...rest], len = 0) => | |
!first | |
? len | |
: getLength(rest, len + 1) | |
// ES5 | |
function getLength2 (arr) { | |
if (arr[0] === undefined) return 0 | |
else { | |
var rest = arr.slice(1) | |
return 1 + getLength2(rest) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment