Learning JavaScript 2

Following my first attempt at the name game, I released that my hint code was rubbish (but really, I knew that at the time) and that it's very difficult to guess a random name with so few clues.

First of all, the code has been improved. It will tell you how many letters you have correct from the first letter, ie first 3 letters or first 100 letters. Secondly, as additional help it can tell you every letter that is correct. This major addition should make guessing from scratch much easier. Version 2 of the name game never made it online as I was already working on version 3. Version 3 got updated to v4 since being posted!

Guess The Name Game v4

Make a guess at the name:

 
 
 
 

What's different?

Most noticeably the hints now come up one at a time, this allows you to choose how much help you get.

You can grab the new JavaScript source from here: namegame4. The source is commented to make it easier to follow and also includes the older revisions within comments for easy comparison. The following is my revised 'beginning of word' check. Not only is it a fraction of the original code but also has no limit on length of word it can check for:

var i=0;
do
	{
	var no3 = "first "+i+" letters correct.";
	i++;
	}
while (result.substring(0,i)===answer.substring(0,i) && i<=AL);

if(i===2){
	var no3 = "first letter correct.";
};

This was my 'which letters are correct' check. Additionally, my first dealing with arrays and was limited to the first 15 letters due to my pre-filled array which contained the names (1st, 2nd etc):

var j=0;
var k=0;
var orderLetters = ["1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th"]
var correctLetters=new Array();

while (j+1<=AL && j<16){
	if (result.substring(j,j+1)===answer.substring(j,j+1)){
		correctLetters[k] = orderLetters[j];
		k++;
	};
j++;
};

My improved letter checking had no length limit as it dynamically works out the st's, nd's and rd's. For the sake of looking pretty, it even put commas between entries and writes and between the final ones. The dynamic naming was done with if else statements:

var l = (j+1).toString();
if (l==="11"){
	var orderLetters = "11th";
} else if (l==="12"){
	var orderLetters = "12th";
} else if (l==="13"){
	var orderLetters = "13th";
} else {
	var l = (j+1).toString();
	if (l.substring(l.length-1)==="1"){
		var orderLetters = (l+"st");
	} else if (l.substring(l.length-1)==="2"){
		var orderLetters = (l+"nd");
	} else if (l.substring(l.length-1)==="3"){
		var orderLetters = (l+"rd");
	} else {
		var orderLetters = (l+"th");
	};
};

So, version 4 has had the if else statements 'upgraded' to switch case statements. This was kindly pointed out by @drJamesThompson. So finally the full code for this hint is:

while (j+1<=AL){
	if (result.substring(j,j+1)===answer.substring(j,j+1)){
		var l = (j+1).toString();
		switch(l)
			{
			case "11":
				var orderLetters = "11th";
				break;
			case "12":
				var orderLetters = "12th";
				break;
			case "13":
				var orderLetters = "13th";
				break;
			default:
				switch(l.substring(l.length-1))
				{
					case "1":
						var orderLetters = l+"st";
						break;
					case "2":
						var orderLetters = l+"nd";
						break;
					case "3":
						var orderLetters = l+"rd";
						break;
					default:
						var orderLetters = l+"th";
				};
			};
		correctLetters[k] = orderLetters;
		k++;
	};
j++;
};

//this makes the hint look pretty if there are 0 or 1 correct letters.
if (k===0){
	var no4 = "";
} else if (k===1){
	var no4 = correctLetters;
} else {
	var no4 = correctLetters.slice(0,-1).join(", ")+" and "+correctLetters[correctLetters.length-1];
};

Again due to my lack of experience in JavaScript there could be room for improvement. Although this time, hopefully, more subtle. If you have the time, please point out improvements in the comments.

Related Posts

Leave a Reply