Skip to content

Instantly share code, notes, and snippets.

@jafow
Last active June 16, 2016 00:46
Show Gist options
  • Save jafow/6402734e3daf1056ce80d56825c59cf7 to your computer and use it in GitHub Desktop.
Save jafow/6402734e3daf1056ce80d56825c59cf7 to your computer and use it in GitHub Desktop.
getLength
// 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