In my spelling game there are different size words from 3-6. Once a certain amount of words are completed in the grid the rest of the grid fades away. I want it to add up all the word size varieties rather than just taking one in to consideration. For example I have it at the moment to fadeOut once two words are completed. The problem is it only fades when two, three letter words are complete or two four letter words are complete etc.
var completeLetters = $('.wordglow2').length;
var threeLetter = (completeLetters / 3);
var fourLetter = (completeLetters / 4);
var fiveLetter = (completeLetters / 5);
var sixLetter = (completeLetters / 6);
if (threeLetter == 2) {
$('table').fadeOut(2000);
}
if (fourLetter == 2) {
$('table').fadeOut(2000);
}
if (fiveLetter == 2) {
$('table').fadeOut(2000);
}
if (sixLetter== 2) {
$('table').fadeOut(2000);
}
Is there not a way of having a variable that combines all the words and then creating an if statement for that?
Firstly – I would advise that you are doing this the wrong way – you are determining game state from the HTML/CSS representation of the game. I would recommend that you create an object which represents the game state and then use this object to determine the manipulations you need to make to the HTML/CSS representation. This will also make doing what you are trying to do here much easier.
For example you may end up being able to say something like…
…which is a lot more pleasant than digging through the HTML. But I digress…
Ok so the class
wordglow2gets added to a letter when it is correct. Each<tr>in the grid can contain a word. So…The above function will look at each row in the game table and count the number of
wordglow2cells. If this is between 3 and 6 then an overall counter is incremented and also counter for each word length is incremented.It can be used as follows…
UPDATE
http://jsfiddle.net/HZX6k/6/
This fiddle just uses a global object to track the gamestate incrementing the count and the count for each of the 3,4,5,6 letter count words as it goes.