aemkei / jsfuck

Write any JavaScript with 6 Characters: []()!+
jsfuck.com
Do What The F*ck You Want To Public License
8.07k stars 671 forks source link

Optimize hyphen #89

Closed frobinsonj closed 5 years ago

frobinsonj commented 5 years ago

Managed to save 15 characters here.

I am a bit confused why ((.0000001)+"")[2] doesn't work here. It returns "-" in all browsers and node.js but returns 0 in tests. Any idea why?

aemkei commented 5 years ago

Hmm. That is strange. .0000001 would be 1e-7 and .00000001 is 1e-8.

hazzik commented 5 years ago

It’s because you unwrapped the number from +[].

The expressions inside the definitions are not real JavaScript expressions

frobinsonj commented 5 years ago

@hazzik, if that is the case, why does it work with 7 zeros and not 6? (((.00000001)+"")[2] and not ((.0000001)+"")[2])

Another option would be to just use (+((+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]+[+[]]+[+[]]+[+[]]+[+[]]+[+[]]+[+[]]+[+!+[]])+[])[!+[]+!+[]] which saves a further 3 characters (18 in total)

hazzik commented 5 years ago

if that is the case, why does it work with 7 zeros and not 6? (((.00000001)+"")[2] and not ((.0000001)+"")[2])

Because if there are 6 zeros the number gets converted to 0.000001, eg it loses one 0 (because somewhere the string concatenation is replaced with a sum). With 6 zeros the number becomes "1e-7" (instead of "1e-8").

hazzik commented 5 years ago

So, this works: '(+(.+[0000001])+"")[2]' and saves 3 characters.

hazzik commented 5 years ago

So, this is this: (+(.0000001)+"")[2]

(+((+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]+[]+[+[]]+[+[]]+[+[]]+[+[]]+[+[]]+[+!+[]])+[])[!+[]+!+[]]
_________________________________________________________the error is HERE ^^^^

And this is (+(.0000001)+"")[2]

(+((+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]+[]+[+[]]+[+[]]+[+[]]+[+[]]+[+[]]+[+[]]+[+!+[]])+[])[!+[]+!+[]]
___________________________________________________________same error HERE ^^^^

And this is correct version: (+(.+[0000001])+"")[2]

(+((+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]+[+[]+[+[]]+[+[]]+[+[]]+[+[]]+[+[]]+[+!+[]]])+[])[!+[]+!+[]]
______________________________________________________________no error HERE ^^^^

The error happens because of the number replacer here: https://github.com/aemkei/jsfuck/blob/977aeeff2a924a1747baf0ee34738f435fdcec05/jsfuck.js#L184

frobinsonj commented 5 years ago

Thanks for the explanation @hazzik :)

I will update the branch.

frobinsonj commented 5 years ago

@aemkei, I've updated the branch. Thanks again hazzik.

aemkei commented 5 years ago

Perfect. Guess we once need to add an explanation to the README about how the character mapping works.