Learning JavaScript – Name Game

When I was younger I found it really interesting you could piece together 'tags' to make web pages. More recently, I've found it fascinating how the guys at Cyanogenmod and ModaCo can hack through source code for phones. They improve not only the speed but also the functionality of android phones sometimes far beyond where the original manufacturer gave up. My example is my old HTC Magic which was running Android 2.1 reasonably well thanks to Cyanogen and team but HTC left me high and dry on Android 1.6!

So the point is, I'd love to be able to do that too. I told myself while I was in Canada I would learn Java but never got round to it (busy life!). Since then I've been spending my time filling in job applications...

Lifehacker brought my attention to Codecademy, an interactive teach-yourself site currently focused on javascript, so I decided to give it a bash. Within a few minutes I was hooked and completed the whole thing in one sitting. Not terribly long but it put wood on the fire. I decided to do something with what I learnt and the result is below.

I just hope that Codecademy update with more detailed lessons in more languages in the near future.

Guess The Name Game

Make a guess at the name: (please click the button)

 
 

If you can guess the name correctly you are able to change it for someone else using your computer.

The javascript source is here. The game can give hints on length:

  • correct
  • too big
  • too small

Various references to correct letters:

  • first 1-5 letters correct
  • second (but not first) correct
  • third (but not first or second) correct
  • first letter incorrect (by deduction this means first three are wrong)

I have inserted below the code for the letter checking. I'm pretty sure there is a much better way to do this, if you have the time please leave some improved code (or a reference to some) in the comments. There are a couple of small bugs eg if you type in the correct three letter answer it will tell your the first 4 letters are correct!

if(result.substring(0,5)===answer.substring(0,5)){
var no3 = "first 5 letters correct";
} else if(result.substring(0,4)===answer.substring(0,4)){
var no3 = "first 4 letters correct";
} else if(result.substring(0,3)===answer.substring(0,3)){
var no3 = "first 3 letters correct";
} else if(result.substring(0,2)===answer.substring(0,2)){
var no3 = "first 2 letters correct";
} else if(result.substring(0,1)===answer.substring(0,1)){
var no3 = "first letter correct";
} else if(result.substring(1,2)===answer.substring(1,2)){
var no3 = "second letter (but not the first) correct";
} else if(result.substring(2,3)===answer.substring(2,3)){
var no3 = "third letter (but not the first 2) correct";
} else if(result.substring(0,1)!=answer.substring(0,1)){
var no3 = "first letter incorrect";}

Finally I hope you enjoyed my first attempt at using JavaScript.

Credits

It would not have been possible without the following sites:

Related Posts

Leave a Reply