28
6
Background
One of the commonly meme'd aspects of javascript is its incredibly loose type coercion that allows for +!![]+[+[]] == '10'
this technique can also be used to create letters as seen in the following example:
[[][[]]+[]][+[]][-~[]] == 'n'
[undefined+[]][+[]][-~[]] // [][[]] -> undefined
['undefined'][+[]][-~[]] // undefined+[] -> 'undefined'
['undefined'][0][-~[]] // +[] -> 0
'undefined'[-~[]] // ['undefined'][0] -> 'undefined'
'undefined'[1] // -~[] -> -(-1) -> 1
'n'
Some examples of js coercions:
[][[]] = undefined // out of bounds array access returns undefined
+[][[]] = NaN // the unary + operator coerces to numbers +undefined is NaN
+[] = 0 // empty arrays coerce to the number 0
~[]/[] = -Infinity // ~ coerces any value to a number then does bitwise negation and / coerces the second value to a number
[]+{} = '[Object object]' // plus coerces both values into strings if it can't do numeric addition
![] = false // ! coerces empty arrays to false
!![] = true // negation of previous coercion
+!![] = 1 // number coercion of true
-~[] = 1 // ~ coerces [] to 0 and the bitwise negation of 0 is -1, -(-1)=1
true+[] = 'true' // adding an empty array to a primitive coerces it to a string
'true'[+[]] = 't' // strings are treated as arrays by js
'true'[-~[]] = 'r'
Here's a reference table of js coercions using different operators
Note: blue shaded cells in the table are strings while yellow cells are literals.
Task
Given a string with only the letters abcdefijlnorstuy
, convert it to valid javascript containing only the characters []+-~/{}!
that evaluates to the original string. Note that this is not JSFuck but is instead focused purely on a subset of strings that can be (relatively) concisely translated.
Input
Any lowercase string that can be written with the letters abcdefijlnorstuy
(can include spaces)
Output
Valid javascript containing only the characters []+-~/{}!
that will evaluate to the original word in a js environment.
Examples
f('u') -> [[][[]]+[]][+[]][+[]]
f('a') -> [+[][[]]+[]][+[]][-~[]]
f('t') -> [-~[]/[]+[]][+[]][-~[]-~[]-~[]-~[]-~[]-~[]]
f('o') -> [[]+{}][+[]][-~[]]
f('l') -> [![]+[]][+[]][-~[]-~[]]
f('r') -> [!![]+[]][+[]][-~[]]
f('defined') -> [[][[]]+[]][+[]][-~[]-~[]]+[[][[]]+[]][+[]][-~[]-~[]-~[]]+[[][[]
]+[]][+[]][-~[]-~[]-~[]-~[]]+[[][[]]+[]][+[]][-~[]-~[]-~[]-~[]-~[]]+[[][[]]+[]][
+[]][-~[]]+[[][[]]+[]][+[]][-~[]-~[]-~[]]+[[][[]]+[]][+[]][-~[]-~[]]
f('aurora borealis') -> [+[][[]]+[]][+[]][-~[]]+[[][[]]+[]][+[]][+[]]+[!![]+[]][
+[]][-~[]]+[[]+{}][+[]][-~[]]+[!![]+[]][+[]][-~[]]+[+[][[]]+[]][+[]][-~[]]+[[]+{
}][+[]][-~[]-~[]-~[]-~[]-~[]-~[]-~[]]+[[]+{}][+[]][-~[]-~[]]+[[]+{}][+[]][-~[]]+
[!![]+[]][+[]][-~[]]+[[][[]]+[]][+[]][-~[]-~[]-~[]]+[+[][[]]+[]][+[]][-~[]]+[![]
+[]][+[]][-~[]-~[]]+[[][[]]+[]][+[]][-~[]-~[]-~[]-~[]-~[]]+[![]+[]][+[]][-~[]-~[
]-~[]]
f('code tennis') -> [[]+{}][+[]][-~[]-~[]-~[]-~[]-~[]]+[[]+{}][+[]][-~[]]+[[][[]
]+[]][+[]][-~[]-~[]]+[[][[]]+[]][+[]][-~[]-~[]-~[]]+[[]+{}][+[]][-~[]-~[]-~[]-~[
]-~[]-~[]-~[]]+[-~[]/[]+[]][+[]][-~[]-~[]-~[]-~[]-~[]-~[]]+[[][[]]+[]][+[]][-~[]
-~[]-~[]]+[[][[]]+[]][+[]][-~[]]+[[][[]]+[]][+[]][-~[]]+[[][[]]+[]][+[]][-~[]-~[
]-~[]-~[]-~[]]+[![]+[]][+[]][-~[]-~[]-~[]]
f('js bad') -> [[]+{}][+[]][-~[]-~[]-~[]]+[![]+[]][+[]][-~[]-~[]-~[]]+[[]+{}][+[
]][-~[]-~[]-~[]-~[]-~[]-~[]-~[]]+[[]+{}][+[]][-~[]-~[]]+[+[][[]]+[]][+[]][-~[]]+
[[][[]]+[]][+[]][-~[]-~[]]
f('obfuscate') -> [[]+{}][+[]][-~[]]+[[]+{}][+[]][-~[]-~[]]+[[][[]]+[]][+[]][-~[
]-~[]-~[]-~[]]+[[][[]]+[]][+[]][+[]]+[![]+[]][+[]][-~[]-~[]-~[]]+[[]+{}][+[]][-~
[]-~[]-~[]-~[]-~[]]+[+[][[]]+[]][+[]][-~[]]+[-~[]/[]+[]][+[]][-~[]-~[]-~[]-~[]-~
[]-~[]]+[[][[]]+[]][+[]][-~[]-~[]-~[]]
Rules
Lowest byte count wins.
The solution can be written in any language, but the generated code must evaluate in either chrome, firefox, or a node.js repl.
Don't abuse any common loopholes.
2This is wonderful. Thanks. Are the other letters impossible to get with
[]+-~/{}!
? – Eric Duminil – 2020-01-25T10:19:10.9304
Yeah, adding () allows you to get all letters since it allows you to create and call functions to get new strings, this is the concept behind JSFuck. But that becomes even more dependent on JS knowledge than this question already is.
– begolf123 – 2020-01-25T14:44:41.4871
f('js bad')
made me chuckle – Tvde1 – 2020-01-27T11:33:54.020