POW – Fixing Compilable JavaScript Code

Many JavaScript/JScript developers use Google Closure Compiler to compile their code. Even though this tool is a great way of minifying JavaScript code, believe it or not, it doesn’t always work (at least as of May 14, 2012). The following JavaScript code works fine when you run it without being minified:

function b() {
  return a--;
}

var a = 1;
alert(b() < !--a);​  // false

Unfortunately, the following minified code presents an issue:

function b(){return a--}var a=1;alert(b()<!--a);

Due to the “<!--” character sequence being present, the JavaScript code never runs. In order to fix the issue, it is requested that no characters be taken away. The functionality of the function should not be changed. No parentheses or brackets should be added. In addition, only a max of three bytes can be added. With the aforementioned criteria in mind, how can you modify the code to make it work after it has been run through the Google Closure Compiler?

The answer to this Problem of the Week will be posted on May 21, 2012.

POW Answer – 0.999999 Repeating Equals One?

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

One of the simplest ways to prove that 0.999999 = 1 is by doing the following:

  • 0.333333 = 1 / 3
  • 3 × (0.333333) = 3 × (1 / 3)
  • 0.999999 = 1

As shown above, since it is true 0.333333 is the way to represent 1 / 3 in decimal form, it follows mathematically that 0.999999 is an alternative way to represent 1 in decimal form. A more extensive discussion can be found here on Wikipedia.

PostgreSQL – Select PL/pgSQL Function Definitions

Something that I did before, but never documented until now was querying the database for all of the PL/pgSQL function definitions. In PostgreSQL, you can run the following to pull all of them along with the owner, schema, function name, and function definition:

SELECT a.rolname AS "owner",
  n.nspname AS "schema",
  p.proname AS "name",
  pg_get_functiondef(p.oid) AS "definition"
FROM pg_language AS l
JOIN pg_proc AS p
ON p.prolang = l.oid
JOIN pg_namespace AS n
ON p.pronamespace = n.oid
JOIN pg_authid AS a
ON a.oid = p.proowner
WHERE l.lanname = 'plpgsql'
ORDER BY "schema" ASC,
  "name" ASC

Of course, you can modify the above query so that you can search for whatever you want based on all of the retrieved fields. If you are still learning about PostgreSQL, I definitely suggest looking through all of the tables, views, and functions in the pg_catalog schema.

POW Answer – Differences In Languages

This is the answer to the Problem of the Week that was posted on April 30.

The desired answer is 0. The reason is because “11” in binary is actually equal to 3. Therefore 3 – 3 = 0. On the other hand, you could use the following table to see how an infinite amount of answers could be given since we don’t necessarily know what base was used for the first translation:

Base Value of 11 Answer
3 4 4 – 3 = 1
4 5 5 – 3 = 2
5 6 6 – 3 = 3
6 7 7 – 3 = 4
7 8 8 – 3 = 5
8 9 9 – 3 = 6
9 10 10 – 3 = 7
X X + 1 X + 1 – 3 = X – 2