Point Zero

Sometimes, it’s good to get back to basics.  I look at code daily- writing new features or fixing/updating old features or trying to figure out what the hell I was thinking when writing a different feature.  It’s been a couple years- you’d think I’d have the basics down by now.

But it’s really easy to lose track of the basics when you’re staring down the tunnel of production code.  When you’re building something in a framework (like Angular 2- which we’re currently using), you don’t have daily interaction with receiving command line input in your program, or figuring out how to deal with floating point numbers in Javascript.  So, I try to do a few online programming problems (from sites like codewars.com or hackerrank.com) every day.  It’s important to keep the basics at least in some dusty corner of your mind for future reference.

Because work sometimes has me writing Javascript, sometimes Python, sometimes C#, and sometimes anything else, I try to do these in multiple languages.  They’re simple problems and should only take a couple minutes each, right?

Wrong.  Turns out, just because a problem is marked ‘easy’ doesn’t mean it’s easy in every language.  For example: one of the easy questions had us taking in some arguments, then adding some other info to those.  So, if we were passed [4, 4.0, ‘hello’], we would add [4, 4, ‘ world’] to those to get the correct answers.  The correct answers in this case being [8, 8,0, ‘hello world’].

Sounds easy so far.  The trouble is with 4.0 + 4.  In any typed language (all the ones I work in but JS), you just make sure your 2nd argument is cast as a Double or Float, and your answer is cast as a Double or Float, and you’re good to go- you get 8.0.  A few lines of code and you’re good to go.

But JS doesn’t really have multiple number types- it has Number.  For any normal floating point number (something like 2.14), it works just fine.  But if the only number trailing your decimal is zero, it gets removed (because unless you have a type that states this number should have trailing zeros, 8 === 8.0 as a number).

So, I tore through C#, Python, and PHP, but got stuck on JS.  Every time, the 2nd item returned 8, but the correct answer was 8.0.  So an ‘easy’ question about data types and simple addition operators, became an exercise in string and array manipulation as well.  Not advanced topics by any means, but it does go to highlight that some languages are better suited for different tasks.

To finally ‘solve’ the problem in JS (because the response had to print out 8.0, not 8), I converted the number to a string and split it into an array on the decimal point.  So, you get [8, 0] for 8.0.  Then you can just check to see if the item at position 1 is 0 – if so, do your calculations, then append the trailing 0 back onto the string.  It’s not pretty- and I bet there’s a better way, but this does work (e is your original submitted string, d is the number you’re supposed to be adding):

var afterDecimal = e.split('.')[1];
if(afterDecimal == 0) {
    console.log(d + +e + '.' + afterDecimal);
} else {
    console.log(d + +e);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s