-3

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);
Liam
  • 27,717
  • 28
  • 128
  • 190
Hugo Buff
  • 411
  • 5
  • 14
  • 2
    would aid people greatly if you changed all the variable names from `_` to `a, b, c` or similar for readability – Kevin L Aug 26 '16 at 13:03
  • 1
    Is this code golf? – Liam Aug 26 '16 at 13:05
  • 1
    Seems kids were playing on the keyboard – B001ᛦ Aug 26 '16 at 13:05
  • @KevinL I assume that's part of the schtick with this. It's supposed to be hard to read – WillardSolutions Aug 26 '16 at 13:05
  • 1
    @EatPeanutButter why would you make the question hard to read if you're trying to see if it returns 800 v 773.06 b/c of floating point errors? – Kevin L Aug 26 '16 at 13:07
  • @KevinL I'm pretty sure this is a [code golf question](http://codegolf.stackexchange.com/). The idea is to create a program to perform a general task in as few a bytes as possible. Either that or it's been [obfuscated](https://en.wikipedia.org/wiki/Obfuscation_(software)) to prevent someone from tampering with it. The OP needs to give some context here – Liam Aug 26 '16 at 13:08
  • 2
    It's probably an obfuscation competition entry. :) It's basicallymore or less (71 * 10) + 21 + ((10 / 38 * 10 + 38) / 10) + 38, which gives me 710 + 21 + 40,63 + 38 = 809,63 But I probably used 38 instead of 20 since I cba to count the _bars. :) – Shilly Aug 26 '16 at 13:10
  • @KevinL I gave the code as was given to me. I'll tidy it up – Hugo Buff Aug 26 '16 at 13:10
  • Your YO is an ugly number "yo": a / bbb * a + bbb – mplungjan Aug 26 '16 at 13:11
  • So @HugoBuff why? Why do you need to do this? Is it an interview question puzzle what? – Liam Aug 26 '16 at 13:11
  • 1
    @Liam I don't *need* to do this, it was given to me I guess as a puzzle but I'm not convinced the answer I was given is correct. Just a piece of interest for me – Hugo Buff Aug 26 '16 at 13:14
  • Could of saved everyone a lot of confusion by explaining this from the off. – Liam Aug 26 '16 at 13:17
  • 1
    Can you explain why you think this should return >800? – Kevin L Aug 26 '16 at 13:18
  • @KevinL To be honest with you, I don't. That's the 'answer' that was given to me, but I wanted a second opinion before I drove myself mad – Hugo Buff Aug 26 '16 at 13:22
  • So this question makes no sense...Your asking why it doesn't return 800, well becasue it doesn't. – Liam Aug 26 '16 at 13:23
  • 1
    @Liam I'm asking "Am I right to think this gives 773.06..?". – Hugo Buff Aug 26 '16 at 13:25

3 Answers3

3

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);
Thomas
  • 11,958
  • 1
  • 14
  • 23
2

Last line is equivalent to : 71*10+21+(10/38*10+38)/10+38 which is 773.06...

You can get 809.63157894 if you forget to divide d by a

Faibbus
  • 1,115
  • 10
  • 18
2

Actually there is no error. This is deliberately obfuscated, but if you simplify you get as follows:

var A = 10;
var B = 21;
var C = 38;
var D = { A: { "yo": A / C * A + C }};
var E = [[[{"heh":{"hehe":[[[[12, 71, 82, 91]]]][0][0][0][1]}}]]];
var F = E[0][0][0].heh.hehe * A + B + D.A.yo / A + C;
console.log(F);

Which then becomes:

var D = 10 / 38 * 10 + 38;
var E = [[[[12, 71, 82, 91]]]][0][0][0][1];
var F = E * 10 + 21 + D / 10 + 38;
console.log(F);

Which then becomes:

var D = 10 / 38 * 10 + 38;
var F = 71 * 10 + 21 + D / 10 + 38;
console.log(F);

Which then becomes:

var F = 769 + (100 / 38 + 38) / 10;
console.log(F);

Which then becomes:

console.log(769 + 3.8 + 10 / 38);
neelsg
  • 4,802
  • 5
  • 34
  • 58