最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How does this code produce "I love you" without any characters or digits? - Stack Overflow

programmeradmin0浏览0评论

Taken from -

How does this produce window.alert("I love you");? I understand how it can obtain I from Infinity, that's about it

    $= ~[];    $={___:
   ++$,$$$$: (![]+"")[$],
    __$:++$,$_$_:(![]+"")
      [$],_$_:++$,$_$$:
        ({} + "")[$],
           $$_$
             :


       ($[$]+"")   [$],_$$:
     ++$,$$$_:(!""+"")[$],$__:
      ++$,$_$:++$,$$__:({}+"")     
       [$],$$_: ++$,$$$:++$,
         $___:++$,$__$:++$
          };$.$_ = ($.$_=
             $ + "")
                [


             $.$_$]+      ($._$=
            $.$_[$.__$  ])+($.$$=(
           $.$+"")[$.__$])+((!$)+"")
           [$._$$]+($.__=$.$_[$.$$_
            ])+($.$=(!""+"")[$.__$
              ]) + ($._= (!""+"")                  
               [ $._$_]) +$.$_
                  [ $.$_$ ]+
                    $.__+


            $._$+    $.$;
          $.$$=$.$+(!""+"")
        [$._$$]+$.__+$._+$.$+
        $.$$;$.$=($.___)[$.$_]
         [$.$_];$.$($.$($.$$+
           "\""+$.$_$_+(![]+
             "")[$._$_]+
                $.$$$_+
                 "\\"+


        $.__$+    $.$$_+
      $._$_+$.__+"(\\\"\\"+
     $.__$+$.__$+$.__$+"\\"+
   $.$__+$.___+(![]+"")[$._$_]+
   $._$+"\\"+$.__$+$.$$_+$.$$_+
    $.$$$_+"\\"+$.$__+  $.___+
      "\\"+$.__$+$.$$$+$.__$+
        $._$+$._+".\\\"\\"+
          $.$__  +$.___+
            ")"+ "\"")
              ( ))(
                );

Taken from - http://www.echojs./news/8518

How does this produce window.alert("I love you");? I understand how it can obtain I from Infinity, that's about it

    $= ~[];    $={___:
   ++$,$$$$: (![]+"")[$],
    __$:++$,$_$_:(![]+"")
      [$],_$_:++$,$_$$:
        ({} + "")[$],
           $$_$
             :


       ($[$]+"")   [$],_$$:
     ++$,$$$_:(!""+"")[$],$__:
      ++$,$_$:++$,$$__:({}+"")     
       [$],$$_: ++$,$$$:++$,
         $___:++$,$__$:++$
          };$.$_ = ($.$_=
             $ + "")
                [


             $.$_$]+      ($._$=
            $.$_[$.__$  ])+($.$$=(
           $.$+"")[$.__$])+((!$)+"")
           [$._$$]+($.__=$.$_[$.$$_
            ])+($.$=(!""+"")[$.__$
              ]) + ($._= (!""+"")                  
               [ $._$_]) +$.$_
                  [ $.$_$ ]+
                    $.__+


            $._$+    $.$;
          $.$$=$.$+(!""+"")
        [$._$$]+$.__+$._+$.$+
        $.$$;$.$=($.___)[$.$_]
         [$.$_];$.$($.$($.$$+
           "\""+$.$_$_+(![]+
             "")[$._$_]+
                $.$$$_+
                 "\\"+


        $.__$+    $.$$_+
      $._$_+$.__+"(\\\"\\"+
     $.__$+$.__$+$.__$+"\\"+
   $.$__+$.___+(![]+"")[$._$_]+
   $._$+"\\"+$.__$+$.$$_+$.$$_+
    $.$$$_+"\\"+$.$__+  $.___+
      "\\"+$.__$+$.$$$+$.__$+
        $._$+$._+".\\\"\\"+
          $.$__  +$.___+
            ")"+ "\"")
              ( ))(
                );

http://jsbin./OhisIQAW/1/edit

Share Improve this question asked Feb 13, 2014 at 13:27 rickyduckrickyduck 4,08414 gold badges61 silver badges95 bronze badges 9
  • 2 Downvote is not mine, but this looks off-topic for Stack Overflow. You may have better luck on Programming Puzzles & Code Golf. – Frédéric Hamidi Commented Feb 13, 2014 at 13:30
  • 1 It seems very much about programming to me. There must be some string concatenation and variable handling in there somewhere. I see what appears to be an array definition at the beginning, but there's no instance of the letter v, for example. – isherwood Commented Feb 13, 2014 at 13:34
  • 1 @FrédéricHamidi Puzzles & Code Golf have remended me to ask it on StackOverflow... what now? – rickyduck Commented Feb 13, 2014 at 13:50
  • 1 @rickyduck, that's unfortunate, I stand corrected. I thought Code Golf was also about reading code, not just writing it. Voting to reopen this, then, my apologies. – Frédéric Hamidi Commented Feb 13, 2014 at 13:56
  • 1 @plalx Thanks for the explanation, but I still don't see how specific characters are declared. – isherwood Commented Feb 13, 2014 at 14:20
 |  Show 4 more ments

3 Answers 3

Reset to default 7

I have translated the whole code:

$ = ~[];                
/*
 * $ = -1
 */

$ = {
    ___:++$,              // $.___  = $ = 0
    $$$$: (![] + "")[$],  // $.$$$$ = "false"[0]           = "f" unuse
    __$: ++$,             // $.__   = $ = 1
    $_$_: (![] + "")[$],  // $.$_$_ = "false"[1]           = "a"
    _$_: ++$,             // $._$_  = $ = 2
    $_$$: ({} + "")[$],   // $.$_$$ = "[object Object]"[2] = "b" unuse
    $$_$:($[$] + "")[$],  // $.$$_$ = "undefined"[2]       = "d" unuse
    _$$:++$,              // $._$$  = $ = 3
    $$$_: (!"" + "")[$],  // $.$$$_ = "true"[3]            = "e"
    $__:++$,              // $.$__  = $ = 4
    $_$: ++$,             // $.$_$  = $ = 5
    $$__: ({} + "")[$],   // $.$$__ = "[object Object]"[5] = "c" unuse
    $$_: ++$,             // $.$$_  = $ = 6
    $$$: ++$,             // $.$$$  = $ = 7
    $___: ++$,            // $.$___ = $ = 8
    $__$: ++$             // $.$__$ = $ = 9
};
/*
 * build digit and data
 */

$.$_ = 
    ($.$_ = $ + "")[$.$_$] +      // $.$_ = "[object Object]"
                                    // "[object Object]"[5]        = "c"
    ($._$ = $.$_[$.__$]) +        // $._$ = "[object Object]"[1] = "o"
    ($.$$ = ($.$ + "")[$.__$]) +  // $.$$ = "undefined"[1]       = "n"
    ((!$) + "")[$._$$] +          // "false"[3]                  = "s"
    ($.__ = $.$_[$.$$_]) +        // $.__ = "[object Object]"[6] = "t"
    ($.$ = (!"" + "")[$.__$]) +   // $.$  = "true"[1]            = "r"
    ($._ = (!"" + "")[$._$_]) +   // $._  = "true"[2]            = "u"
    $.$_[$.$_$] +                 // "[object Object]"[5]        = "c"
    $.__ +                        //                               "t"
    $._$ +                        //                               "o"
    $.$;                          //                               "r"
/*
 * $.$_ = "constructor"
 */

$.$$ = 
    $.$ +                 //             "r"
    (!"" + "")[$._$$] +   // "true"[3] = "e"
    $.__ +                //             "t"
    $._ +                 //             "u"
    $.$ +                 //             "r"
    $.$$;                 //             "n"
/*
 * $.$$ = "return"
 */

$.$ = 
    ($.___)[$.$_][$.$_];  // (0)["constructor"]["constructor"] 
/* 
 * $.$ = function Function(){ [native code] }
 */

$.$(                    // Function(
    $.$(                  //   Function(
        $.$$ +              //     "return
        "\"" +              //     "\""
        $.$_$_ +            //     "a"
        (![] + "")[$._$_] + //     "l"
        $.$$$_ +            //     "e"
        "\\" +              //     "\\"
        $.__$ +             //     "1"
        $.$$_ +             //     "6"
        $._$_ +             //     "2"
        $.__ +              //     "t"
        "(\\\"\\" +         //     "(\\\"\\"
        $.__$ +             //     "1"
        $.__$ +             //     "1"
        $.__$ +             //     "1"
        "\\" +              //     "\\"
        $.$__ +             //     "4"
        $.___ +             //     "0"
        (![] + "")[$._$_] + //     "l"
        $._$ +              //     "o"
        "\\" +              //     "\\"
        $.__$ +             //     "1"
        $.$$_ +             //     "6"
        $.$$_ +             //     "6"
        $.$$$_ +            //     "e"
        "\\" +              //     "\\"
        $.$__ +             //     "4"
        $.___ +             //     "0"
        "\\" +              //     "\\"
        $.__$ +             //     "1"
        $.$$$ +             //     "7"
        $.__$ +             //     "1"
        $._$ +              //     "o"
        $._ +               //     "u"
        ".\\\"\\" +         //     ".\\\"\\""
        $.$__ +             //     "4"
        $.___ +             //     "0"
        ")" +               //     ")"
        "\""                //     "\""
    )()                   //   )()
)();                    // )();
/*
 * Function(
 *   Function(
 *     "return\"ale\\162t(\\\"\\111\\40lo\\166e\\40\\171ou.\\\"\\40)\""
 *   )()
 * )();
 */

also you can read it from here: https://gist.github./mutoo/9003010

It's quite simple to decode actually. Just open up a debugger and and evaluate every statements (in order). To find the statements, look for ;. It will then give you the result of the expressions and will help you to find out how they affect each other and then you can analyse every expression individually to see how they were constructed.

Expression #1

$= ~[]; // -1

Expression #2

$={___:
   ++$,$$$$: (![]+"")[$],
    __$:++$,$_$_:(![]+"")
      [$],_$_:++$,$_$$:
        ({} + "")[$],
           $$_$
             :


       ($[$]+"")   [$],_$$:
     ++$,$$$_:(!""+"")[$],$__:
      ++$,$_$:++$,$$__:({}+"")     
       [$],$$_: ++$,$$$:++$,
         $___:++$,$__$:++$
          };

Here we see some letters started to pop up so let's see how? First let's look at the $$$$ property that references 'f'.

$={___:++$, $$$$: (![]+"")[$],

In the above code, we know that $ was initially -1 and now we see ++$ which means $ is now 0, then we look at (![]+"")[$] which defines the value of the $$$$ property.

(![]+"") //evaluates to 'false'
'false'[$] //which we can translate to...
'false'[0]; //f

Let's now do the next property $_$_, which holds the value 'a'.

__$:++$, $_$_:(![]+"")[$]

We can see again that ++$ increments $ by 1 so we are at 1 now, then it's the same expression as for the letter f, but we are at index 1 so:

'false'[1]; //a

You can use the same approach to find out the rest of the code, good luck!

This is a cool puzzle. It's not that bad when you break it down, but it does rely on a lot of JavaScript quirks. I've done a few lines to show how variables are getting set to integer and characters. If anyone wants to finish this, I would gladly make it a munity wiki.

 $ = ~ []; // $ = -1
 $ = {
     ___: ++$,                // ___ = 0, $ = 0
     $$$$: (![] + "")[$],     // $$$$ = "f";
     __$: ++$,                // __$ = 1, $ = 1
     $_$_: (![] + "")[$],     // $_$_ = "a"
     _$_: ++$,                // _$_ = 2, $ = 2
     $_$$: ({} + "")[$],      // $_$$ = "b"
     $$_$: ($[$] + "")[$],    // $$_$ = "d"
     _$$: ++$,                // _$$ = 3, $ = 3
     $$$_: (!"" + "")[$],
     $__: ++$,
     $_$: ++$,
     $$__: ({} + "")[$],
     $$_: ++$,
     $$$: ++$,
     $___: ++$,
     $__$: ++$
 };

 $.$_ = ($.$_ =
 $ + "")[$.$_$] + ($._$ =$.$_[$.__$]) +
 ($.$$ = ($.$ + "")[$.__$]) + ((!$) + "")[$._$$] +
 ($.__ = $.$_[$.$$_]) +
 ($.$ = (!"" + "")[$.__$]) +
 ($._ = (!"" + "")[$._$_]) + $.$_[$.$_$] +
 $.__ + $._$ + $.$;

 $.$$ = $.$ + (!"" + "")[$._$$] + $.__ + $._ + $.$ + $.$$;

 $.$ = ($.___)[$.$_][$.$_];

 $.$($.$($.$$ + "\"" + $.$_$_ + (![] + "")[$._$_] + $.$$$_ +
         "\\" +
         $.__$ + $.$$_ +
         $._$_ + $.__ + "(\\\"\\" +
         $.__$ + $.__$ + $.__$ + "\\" +
         $.$__ + $.___ + (![] + "")[$._$_] +
         $._$ + "\\" + $.__$ + $.$$_ + $.$$_ +
         $.$$$_ + "\\" + $.$__ + $.___ +
         "\\" + $.__$ + $.$$$ + $.__$ +
         $._$ + $._ + ".\\\"\\" +
         $.$__ + $.___ +
         ")" + "\"")
     ())();
发布评论

评论列表(0)

  1. 暂无评论