JavaScript puzzle - get length of an Array without using length
Jan Küster
Posted on February 20, 2022
Write a function fn
that returns the length of an Array as Number.
Constraints:
- no use of
length
property -
size
and length/size indicating properties of other data structures are also not allowed
Tips:
- try to avoid the comments section, as the solutions are often posted there
- it's way easier than you might think (which is why it's tagged
#beginners
) - read about Array fundamentals
- don't expect the solution to be something that you should ever use in production code
Tests:
console.assert(fn([]) === 0)
console.assert(fn([1]) === 1)
console.assert(fn(['1']) === 1)
console.assert(fn(['1', 2, {}]) === 3)
Bonus:
This is all achievable by using 20 characters (inkl. function declaration) overall.
💖 💪 🙅 🚩
Jan Küster
Posted on February 20, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.