r/ProgrammerHumor • u/_hoh_ • Sep 04 '17
[[][[]]+[]][+[]][++[+[]][+[]]] is "n" in javascript
[[][[]]+[]][+[]][++[+[]][+[]]]This evaluates to "n" in javascript. Why?
Let's start with an empty array
[]Now, let's access a member of it.
[][]What member? Let's check for the empty array member
[][[]]oh, that is undefined. But if we add an empty array to that, it is casted to the string "undefined"
[][[]]+[]Let us wrap that in an array
[[][[]]+[]]We can now try to access letters in that string. First, we must unwrap the string. That can be done by accessing the first element of that array.
[[][[]]+[]][0]0 can be created by casting an empty array to a number:
[[][[]]+[]][+[]]Now, "n" is the second letter in that string, so we would like to access that:
[[][[]]+[]][+[]][1]But how can we write 1? Well, we increment 0, of course. Wrap 0 in an array, and increment the first member of it:
++[0][0]Like before, this is equivalent to
++[+[]][+[]]So our final code is then the glorious
[[][[]]+[]][+[]][++[+[]][+[]]]
View all comments
Show parent comment
13
u/Ultimater Sep 05 '17
The problem with
()[]+is how to produce "r" and "s".If you can find a way to produce "r" and "s" using only
()[]+, you have the entire alphabet: https://jsfiddle.net/7mjjczar/If we increase the character set to 6, we can replace the
!character with any operator that returns a boolean result.For example if we chose
()[]+<then we could do:r:
[[+[]<++[+[]][[+[]]]]+[]][+[]][++[+[]][+[]]]s:
[[+[]<+[]][+[]]+[]][+[]][++[++[++[+[]][+[]]][+[]]][+[]]]Or if we chose
()[]+=then we can could do:r:
[[[]==+[]][+[]]+[]][+[]][++[+[]][+[]]]s:
[[[]==[]][+[]]+[]][+[]][++[++[++[+[]][+[]]][+[]]][+[]]]The
=character could also help reduce bytes in larger code through assignments.We get pretty close with
[+[]]['includes'](+[])but it requires an "s" and "l" we don't have. I'd expect to be able to get "l" fromnull, but unsure how to produce it at this time.Perhaps by:
[[]+[]][+[]]['match'](+[]). But we have no access to "m" or "h". Even if we got "l", we'd still have to figure out how to produce "s".There's also
[[]+[]][+[]]['startsWith']([]+[])but this doesn't help us at all because it requires both an "r" and an "s" as well as a capital "W".There's
[[]+[]][+[]]['endsWith']([]+[])but it doesn't get us anywhere.With a bit of ingenuity, I'm sure it's possible. How hard can it be to produce a boolean in JavaScript? Maybe if we had access to comparison operators like either
&&or||:0 && 0=> 00 || 0=> 0Yeah, those don't help us at all....