Itsy Bitsy Bytes


Crashing the uncrashable

Usually the goal in programming is not to have your code crashing. Usually.

I was mentioning last month that I took a liking to HackeRank and one of the most demanding challenges I ran into was one at the complete beginner / easy level.

Titled Exceptions - String to Integer, it was supposed to teach you about handling exceptions.

The task was very simple ideed: you read a string S (with a length between 1 and 6 characters, composed of either lowercase letters (a−z) or decimal digits (0−9)). You then attempt to convert this string into an integer and if it fails, print Bad String.

How do you do this in Javascript? Very easily.

\\hackerrank's editor reading method
var S = readLine();
try {
    var int_value = parseInt(S, 10);
    \\hackerrank's editor writing method
    process.stdout.write(int_value);
} catch (e) {
    \\hackerrank's editor writing method
    process.stdout.write('Bad String');
}

However, this does not work. Why? Because it would not be a good idea (arguably) to get an exception every time you attempt a string parsing. So in cases likes this Javascript's parseInt returns NaN and does not throw an exception.

I was stuck. My code did not pass...

In my next attempt, I tried to outsmart them by using an ulterior isNaN() detection on the result and manually throwing an exception

var S = readLine();

try {
    var parsedS = parseInt(S, 10);

    var result = isNaN(parsedS) ? 'Bad String' : parsedS;
    process.stdout.write(result)
} catch (e) {;
    process.stdout.write('Bad String');
}

Guess what, this did not work either. According to their terms, I could not use an if / else statement.

Note: You must use the String-to-Integer and exception handling constructs built into your submission language. If you attempt to use loops/conditional statements, you will get a 0 score.

I tried some other tricks to somehow force an exception by using a NaN value but the Math objects had no problems with it (for example, it turns out Math.pow(NaN, Nan) is NaN). Some of the browser's window methods do crash when you use NaN as an argument but I did not have access to any of them, just pure Javascript methods.

So how do you crash an uncrashable method then? I have no idea but I did manage to eventually pass this challenge with flying colors.

As I mentioned before, the input value was composed of either lowercase letters (a−z) or decimal digits (0−9). Using an undefined variable throws an exception and what does a string of lowercase letters looks like? A variable name. This is when I had my Eureka! moment.

You see, there is another method of parsing a string to integer in Javascript. Kinda...

var S = readLine();

try {
    var parsedInt = eval(S);
    process.stdout.write(parsedInt);
} catch (e) {;
    process.stdout.write('Bad String');
}

By using eval on the string I ended up with either an integer, either an undefined variable due to the challenges's input constraints.

I'm still curious though if you can crash Javascript with NaN. So far, I did not found an answer to this question but the quest continues...


comments powered by Disqus