What I’m trying to make is a check list for a lotto game, which is a sort of bingo for my family.
I will first try to explain what the check list does and why, excuse my technical english, I’m dutch, so some words can be wrong 🙂
I have a list with a couple of people who play the lotto/bingo game. All players pick 10 numbers and once a week there is a draw of 6 numbers, I try to explain step by step what the code has to do.
1 – 10 people’s numbers should be checked
2 – 6 numbers are added every week which should be compared with the numbers of the people.
3 – font should be colored green when there is a match.
4 – font should stay red when there is no match
Here is the code I have this far, a live version is at the link.
The code beneath works great, but the problem is that the code is designed to compare var A with var B, this is a bottleneck, because it’s a 1 on 1 action. I can’t add more people without adding a Draw day.
Now my question. What should be done to add more people (A1, A2, A3 etc etc.) without adding a draw date like B2.
I hope this is clear enough. 🙂
<script type = "text/javascript">
var a1 = ["2","3","8","12","23", "37", "41", "45", "48"]
var a2 = ["2","14","3","12","24", "37", "41", "46", "48"]
var b1 = ["2","5", "11","16","23","45", "46"];
var b2 = ["1","23", "11","14","23","42", "46"];
for (var i = 0; i< a1.length; i++)
{
for (var j = 0; j< b1.length; j++)
{
if (a1[i] == b1[j])
{
a1[i]= "g"+ a1[i];
}
}
}
for (var i = 0; i< a2.length; i++)
{
for (var j = 0; j< b2.length; j++)
{
if (a2[i] == b2[j]) {
a2[i]= "g"+ a2[i];
}
}
}
// john
document.write("<font color = '#FFFFFF'>" + "<b>" + "John    " + "</b>");
for (var i = 0; i< a1.length; i++)
{
if (a1[i].substr(0,1) == "g")
{
a1[i] = a1[i].substr(1,20);
document.write("<font color = '#00FF00'>", a1[i] + "    ");
}
else
{
document.write("<font color = '#FF0000'>", a1[i] + "    ");
}
}
// Michael
document.write("<br><br>");
document.write("<font color = '#FFFFFF'>" + "<b>" + "Michael    " + "</b>");
for (var i = 0; i< a2.length; i++)
{
if (a2[i].substr(0,1) == "g")
{
a2[i] = a2[i].substr(1,20);
// The Draw
document.write("<font color = '#00FF00'>", a2[i] + "    ");
}
else
{
document.write("<font color = '#FF0000'>", a2[i] + "    ");
}
}
document.write("<br><br>");
document.write("<br><br>");
document.write("<font color = '#FFFFFF'>" + "<b>" + "Draw day 1 " + "</b>");
document.write("<br>");
document.write("<font color = '#FFFFFF'>" + "<b>" + "Sat 08-08-2009 " + "</b>");
document.write("<br><br>");
for (var j = 0; j< b1.length; j++)
{
document.write("<font color = '#FFFFFF'>", b1[j] + "    ");
}
document.write("<br><br>");
document.write("<br><br>");
document.write("<font color = '#FFFFFF'>" + "<b>" + "Draw day 2 " + "</b>");
document.write("<br>");
document.write("<font color = '#FFFFFF'>" + "<b>" + "Sat 15-08-2009 " + "</b>");
document.write("<br><br>");
for (var j = 0; j< b2.length; j++)
{
document.write("<font color = '#FFFFFF'>", b2[j] + "    ");
}
</script>
In addition to rewriting the code (refactoring) so the array comparison into a function as Miky D did, you can make the comparison more efficient by using an object to hold the winning numbers. Note that this code isn’t the final version; there are further refinements.
By turning the winning numbers into the indices rather than the values, you can get rid of a loop. Also, ‘a’ and ‘b’ aren’t very descriptive names. Short names gain you nothing but obfuscation.
By marking successful and successful guesses differently (currently, you prepend ‘g’ to successes), you can also simplify the code to display the results. The
<font>tag has been deprecated for awhile, so this refinement uses<span>s with a class that you can style.Since
document.writeis also deprecated, I’ll replace it with the modern equivalents,document.createElementandNode.appendChild. If you think the resulting code is too verbose, you can instead useinnerHTML, though its use is controversial. Since player names are closely tied to player picks, I’ll also index the player picks by player name.Put the following in a file named “lotto.js” in the same folder as the web page.
Here’s the corresponding HTML page:
You can refine the CSS to get the exact styling you want. The code could be further refactored to use OOP, which would simplify creating new players and draws, but it’s more involved so I won’t go in to it here.
Update: The above code was rewritten so that each player’s guesses is compared to every draw. The live sample version of the code has been refactored almost beyond recognition to use OOP. It also uses features you probably haven’t seen before, such as JS closures, higher order functions and CSS generated content and counters. It’s longer and harder to understand but more flexible and a little easier to use.