- 2 bytes saved thanks to Neil.
Try it here, or use the Snippet below to test it with
eval instead of
Japt port, 24 bytes
It's not really worth posting this as a separate solution as it's, essentially, identical.
Given that, we therefore don't have the convenience of a constant or method to work with. Instead, we're going to create a function that calls itself continuously before, eventually, crapping out. In it's simplest form that is:
But that's not much use to us for this challenge as it only throws an overflow error with no indication of how many times
f called itself. We can avoid the error by
trying to call
f continuously and
catching when it fails:
No error, but still no return value of how many times the function managed to call itself before failing, because the
catch isn't actually doing anything. Let's try evaluating the
try / catch statement:
Now we've got a value being returned (and, because this is code golf, saved ourselves a few bytes over using an actual
return). The value being returned, though, is
undefined, again because the
catch isn't doing anything. Luckily for us
-~n==n+1 so, by popping a
-~ in front of the call to
f, we've essentially got
-~-~ ... -~-~undefined, with another
-~ prepended with each call, giving us the number of times
f was called.