Viewing javascript | View Recent

Javascript has two string methods (substr and substring) that appear to be identical at first glance; they both return a substring from a given string. So what's the difference? Their second parameters, while both numbers, are expecting two different things.

When using substring the second parameter is the first index not to include:

var s = "string";
s.substring(1, 3); // would return 'tr'

var s = "another example";
s.substring(3, 7); // would return 'ther'



When using substr the second parameter is the number of characters to include in the substring:

var s = "string";
s.substr(1, 3); // would return 'tri'

var s = "another example";
s.substr(3, 7); // would return 'ther ex'



For more information on string functions, checkout Quirksmode's guide.

A while ago when I was hacking around with working on an Event System I wrote a simple Javascript date formating script. The script translated something like '11-02-2008' to something like 'Monday, 2 February 2008' using the Javascript Date object and a few parseInts.

After a while, something seemingly random started happening: any date in September or October displayed the wrong day name. I soon traced my problem back to a call to parseInt().

var m = parseInt(month); // would fail if month was '08' or '09'



For some reason it was causing the months index to become 0. After a bit of Googling I found an answer. The function parseInt tries to be clever and guess which base you are using to convert your string to an integer. Although it usually defaults to base 10, when given strings starting with '0', it assumes that you are parsing an octal number (strings starting with '0x', it assumes hexadecimal).

parseInt('07'); // 7
parseInt('08'); // 0
parseInt('09'); // 0
parseInt('10'); // 10



A workaround is to always pass the radix to parseInt:

parseInt('08', 10);
parseInt('09', 10);



For more information, check out Mozilla's Developer Center.

Feb 3



Before entering my final year at uni I consulted an academic advisor to see if I would graduate when I was expecting to. Both degrees that I was studying (IT with an Interaction Design major, and Multimedia) had been restructured and some subjects that used to be core were now electives and vice versa. As it turned out, some of the subjects that I had already finished thinking they were core were now considered electives so now I had to use up the electives that I had been saving on core subjects. One such subject, MMDS1400 (Intro to Scripting) didn't even exist when I started uni but since it was now a core subject I had to take it.

MMDS1400 is a first year scripting course that introduces students to the basics of html and javascript. The major project for Intro to Scripting was to create a game using javasript (and no AJAX as well :-(). A friend of mine and fellow forth year at the time, Dan Wall was also taking the subject and shared my general sense of ridiculousness that two final year students had a first year subject suddenly becoming mandatory. Because the coursework was so simple we decided to push our major project a bit further than what was required to pass. For our javascript game, we chose to implement Chess.

We set about drawing the pieces (in Inkscape, of course) and writing a quick script to free-form move the pieces around the board. Pretty soon we had a very basic chessboard. Our next move (chess pun? no, just a coincidental choice of words) was to restrict the movement of the pieces to within the rules of the game. We started by writing down all of the moves that each type of piece could make and found any patterns that we could exploit in our code; the moves of the Queen, for example, can be found by combining the moves of a Bishop and a Rook. We wrote in the moves available to each piece, added a move timer and our game was done. We had several known limitations (such as moves wrapping around the board, and no Castling or Pawn promotion) but we felt that we had more than satisfied the requirements of the assessment.

A final part of the assessment was to bundle our game in a website explaining the process of constructing our game. You can view our chess site here.

One of my new years resolutions was to get this blog/folio thing of mine in order so that's what I'm going to start doing. I have gone through all of my old uni projects and uploaded anything worth uploading :-P. Over the next few posts I'll be writing a short piece about a few of the projects that I completed while I was at uni.

First up is a project from a course I took in first year. We were given a week to come up with and create a game that used only rollovers to play. My team was called 'Not the Face' and consisted of Nigel McBryde and Brett Holton, two Multimedia Design students. We began our brainstorming with ideas of some kind of 'find the treasure' game based on Hot and Cold where you are told if you are warm (close) or cold (far) compared to the treasure. We soon found that most of the other teams had similar ideas so we opted for something completely different.

I had a random idea for a game based on a guy dancing. I'm not sure if it could technically be called a game because there are no rules or even any goals. The idea was that you had a guy at the top of the screen and several smaller version of him in varying poses, or dance moves. As the player moused over the smaller guys the main dancer would move to copy their pose. The more the player moved the mouse between each pose, the more the main guy would seem to dance. We found a few midis to throw in as background music (which only works in Internet Explorer, but this was in a time before Firefox :-P) and the game was done.

I think the game was received quite well; we had most people amused for a while. Theo, the lecturer, even kept it for use the following year to show students an example of how the assignment was meant to be done ;-).

Click here to play the game.