JavaScript Operators Challenge Question

JavaScript Operators Challenge Question that I stumbled upon from Stackoverflow and wanted to share it here to have as a fun challenge question.

The question is: In JavaScript, why does this equal 42?

[(0>>(0==0))+([0]+[(0==0)+(0==0)]^0)]*[(0^[(0==0)+(0==0)]+[0])+((0==0)<<0)]

Give it some thought. Have you figured it out yet?

Here is the explanation given at StackOverflow

The basic elements are as follows:

0==0

This is true, which can be coerced in to 1.

a >> b

The right-shift operator. In this case, it’s only used at the beginning of the expression as 0 >> 1 which evaluates to 0.

a^b

Bitwise XOR. Both usages above have either a or b are 0, and so the result is the non-zero operand.

[a] + [b]

String addition of a and b, evaluates to “ab”; if both a and b are numeric (e.g. [0]+[1] the result can be coerced into a numeric.

[a] * [b]

Multiplication can be performed on single element arrays, apparently. So this is equivalent to a*b.

Finally,

a << b

The left-shift operator; for positive integers this effectively multiplies by 2 to the power of b. In the expression above, this is used with b = 0, so the result is a.

If you apply the correct order of operations, you get out [2] * [21] which evaluates to 42.

Send it to your friends, see how good they are with JavaScript operators.

Double Exclamation Mark JavaScript Notation

I find this to be a terribly obscure way to do a type conversion. The double exclamation mark in JavaScript basically means convert to Boolean, invert, then invert again. I documented this on my blog for quick reference as I keep running into it.

So you’re converting a value to a boolean, then inverting it, then inverting it again. Take a look at the three examples below that mean the same thing starting with the double exclamation mark.

// Really Confusing:
site.enable = !!webId;

// Less Confusing:
site.enable = (webId != 0) ? true : false;

// Easiest to understand in my opinion:
site.enable = (webId != 0);


Postfix increment and decrement operators in CSharp

Postfix increment and decrement operators are great for doing quick addition and subtraction. Using your C# knowledge and not your compiler, what will the output be?

int i = 1;
string result = String.Format("{0},{1},{2}", i--, i++, i++);
Console.WriteLine(result);

Have your guess?

The output reads as follows. 1,0,1 and the last value of i is actually 2. Why is this?  We are using post operators, meaning, the compiler is going to read the value of i, then it will increment or decrement depending.  In the above example, the last i++ happens after the value of {2} is read, so it will not be seen as 2 in the console window and is not used in any execution path.  So let’s take a look at the opposite of above.

int i = 1;
string result = String.Format("{0},{1},{2}", --i, ++i, ++i);
Console.WriteLine(result);

Have your answer for this version?

The output ends up being 0,1,2 with a final value of i =2.  The reasoning is opposite of the above.  These operators will apply the increment or decrement prior to reading i.


StackOverflow Profile