Category Archives: Javscript – the good – the bad and the ugly.

School holidays are a great time for kids to well, have a holiday.

But this year, this poor programmer has been besieged by some pupils of a local School. The poor things had been set the task of doing the javascript course on the site.

It’s a big course. I was a bit dubious about some of it’s teaching methods though. One poor lad was stuck for 24 hours when he wrote a lovely infinite loop. Instead of the Code Academy site working that out, it just let the code hang the browser. The lad thought his computer had gone wrong.

I also detected a large amount of copy and paste. There is  a help forum which is very copy and pastey and I wondered about how much javascript they were REALLY learning and how much of CTRL C/V was going on.

I noticed with loops (and this is a minor point) they did not point out it is better to do:-

var a=b.length;
for (var c=1;c<a;++c)


for (var c=1;c<b.length;++c)

Because the for next loop is reading the b.length over and over. Ok I’m being picky but.

I’m not sure about storing functions in variables either.

var x=function() {
 console.log ("Hello");

as opposed:-

function x() {

The first example is 97% slower running than the second. That’s a whole lot of slower. (Credits to this link)

I did like the way it taught object oriented programming and different notations although the student I worked through it with got very confused by it. But point taken it’s probably really good to learn all three things.

But my main bug bear was really that you would get code that would fail even though it was correct. In one exercise the output of J.R.R Tolkien vs J.R.R. Tolkien caused the code to fail. Technically the code was right so it should have passed. Yes, there was a slight difference in the output from what was required but I get the impression the test of whether the code works is not able to actually tell if the code is right but only what output is given under certain circumstances, it’s like JUnit testing where you write a test in java that really doesn’t actually test the code but just the output. Although the student didn’t notice at one point I did manage to fool the tester into thinking I’d written correct code just by “console.logging” what I thought it wanted to hear.

Overall it’s a very good concept, the site is laid out well and nice to use. *BUT* I think it’s inappropriate to throw it at a complete new programmer (who is 14) without much help. At worst it teaches you to Copy and Paste. At best you actually learn to code.

Lastly I wondered why thee was no interaction with the web itself, Javascript is essentially a web technology but there was no effort to give any more output than “console.log”. Sure, you learn javascript but all you can do is put up input boxes and output to a console. Maybe at the end they could have introduced web technologies like JQuery as  a pre-cursor to a javascript course.

Yup, it’s a great site, I love what it’s trying to do, but the big no no for me is the hanging when a student writes an infinite loop, it can’t be that hard to write a “code timeout” that says if it’s not executed within a set amount of time give the “infinite loop” warning. Also, some of those error messages just don’t really help!. At one point I was going to get the student to install NetBeans and paste the javascript in there as it would pretty much help them to get it right. All the students I worked with kept doing this:-

for (var i=0;i<k.length;++i) ;

they would get very confused about when you { or ( or [ rather than ;.

Anyway – we got there in the end. Perhaps I should become a teacher?


Javascript isDate function for UK

Us splendid chaps in the UK have our date formatted  dd-mm-yyyy, whilst this is obviously the correct way one should speak, it means that most is date functions for javascript don’t work for us.

Here’s one I found someone else modified for the UK. Splendid!


function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
return false;

month = matchArray[3]; // p@rse date into variables
day = matchArray[1];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn`t have 31 days!")
return false;

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn`t have " + day + " days!");
return false;
return true; // date is valid