The following answers the Problem of the Week from May 14, 2012:

At first glance, it may seem that the following would be the solution:


function b() {
  return a--;
}

var a = 1;
alert(!--a > b());

Unfortunately, the above results in true being returned. In order to get false to be returned, you could do the following:


function b() {
  return a--;
}

var a = 1;
alert(!a-- > b());

Even though the result is the same as far as the alert is concerned, it is still not quite right because the original alert was really evaluating to this:

alert(1 < !-1);  // alert(b() < !--a)

while the proposed solution’s alert evaluates to this:

alert(!1 > 0);  // alert(!a-- > b())

In order to get something that acts just like the original, without allowing Google Closure Compiler to mess it up, we can do the following:


function b() {
  return a--;
}

var a = 1;
alert(b() < !+--a);

That’s right, all we have to do is add the plus sign and we are good to go! 8)


1 Comment

POW – Fixing Compilable JavaScript Code | Chris West's Blog · May 29, 2012 at 6:43 PM

[…] Post navigation ← Previous Next → […]

Leave a Reply

Your email address will not be published. Required fields are marked *