Search code examples
javascriptarithmetic-expressionsdeobfuscation

Is there a floating point error in this obfuscated code?


As a puzzle I was given the following snippet of code yesterday with the question

Why does this give 773.06..?

var _ = 10, __ = 21, ___ = 38;
var _______ = { _: { "yo":_/___*_+___ }};
var ___________ = [[[{"heh":{"hehe":[[[[12,71,82,91]]]][0][0][0][1]}}]]];
var ____________ = ___________[0][0][0].heh.hehe*_+__+_______._.yo/_+___;
console.log(____________);

I can't remember the expected answer, but it was at least 800. A colleague looked at it briefly and said it was due to floating-point imprecision but I think that it should return 773.06..

What is the correct answer?

Tidied code

var a = 10, b = 21, c = 38;
var d = a/c*a+c;
var e = (71*a)+b+(d/a)+c;
console.log(e);


Solution

  • let's just remove the bloat, and give this thing a few readable variable names:

    //translated line by line
    var a = 10, b = 21, c = 38;
    var d = a/c*a + c;
    //d = 100/38 + 38
    //d = 40.63157894736842
    var e = [12,71,82,91][1];
    //e = 71;
    var f = e*a + b + d/a + c;
    //f = 710 + 21 + 4.xxx + 38
    //f = 773.0631578947368
    console.log(f);