The Hazards of Programming

As the new Geogad web site is starting to take form, I sometimes look back in frustration and wonder why it is taking so long. And then today, I got a small reminder.

Part of the new site will use Javascript, in particular, a page that lets the user set dates. The code seemed to be working well until I tested setting the months of August and September. Then the code completely broke.

The key was that the month value was being read in correctly from the input box, but the conversion from a string value to an integer was not returning the correct number. The function that converts a string to an integer in Javascript is parseInt. In my case, instead of getting “08″ or “09″ converting to 8 or 9, I was getting 0. Turns out the problem is that Javascript can get confused in its conversion. It assumes numbers that begin with a “0″ are octal (or base-8) numbers, where I was assuming that everything was in decimal (or base-10) numbers.

Thanks to a quick surf of the Internet, I found that the solution was to specify base-10 in all my conversions. So my original, incorrect statement

number = parseInt(”08″);

is now

number = parseInt(”08″, 10);

Thank goodness for the Internet. It just saves programmers hours and hours of time.

Technorati tags: Javascript, parseInt, string conversion

Tags: , ,

Leave a Reply

You must be logged in to post a comment.