tags:

views:

191

answers:

5

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 &nbsp&nbsp " + "</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] + " &nbsp&nbsp ");
    }
    else
    {
        document.write("<font color = '#FF0000'>", a1[i] + " &nbsp&nbsp ");
    }
}

// Michael
document.write("<br><br>");
document.write("<font color = '#FFFFFF'>" + "<b>" + "Michael &nbsp&nbsp " + "</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] + " &nbsp&nbsp ");
    }
    else
    {
        document.write("<font color = '#FF0000'>", a2[i] + " &nbsp&nbsp ");
    }
}
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] + " &nbsp&nbsp ");    
}
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] + " &nbsp&nbsp ");
}
</script>
+1  A: 

you should use === as comparison operator

For example let x = 5:

x == 8     // yield false

whereas:

x == "5"   // yield **true**

=== is like saying "is exactly equal to" (by value and type)

x === 5    // yield true
x === "5"  // yield false
dfa
A: 

I really don't knw how to implement your hint. I'm pretty new to javascript. Would you please be so good to make an working example of how to add a A var which so we have 2 A vars checked by 1 B var with output?

Chris
In the future, use the comment feature to comment on an answer. Don't answer your own questions unless you actually have found the answer to the question.
Sinan Taifour
@Sinan: I almost chided Chris for this, until I noticed he only has 11 rep. In this case, the thing for him to do is edit his original question, right?
outis
@ Sinan and Outis, this is differnt then forums I see before, excuse me for my ignorance, this concept is new to me.
Chris
A: 

Here's a few things that I noticed:

  • you could use array's of arrays to store the number picks and the winning numbers
  • you could really create some functions to do the repeated work
  • the HTML code that you render from your JS is not properly formed

Use functions to perform repeated code

function checkArray(myValues, winningValues)
{
   for (var i = 0; i< myValues.length; i++) 
   {
      for (var j = 0; j< winningValues.length; j++) 
      {
        if (myValues[i] == winningValues[j]) 
        {
          myValues[i] = "g"+ myValues[i];
        }
      }
   }
}

function displayNumbers(playerName, myNumbers)
{
    document.write("<font color = '#FFFFFF'>" + "<b>" + playerName + " &nbsp&nbsp " + "</b></font>");
    for (var i = 0; i< myNumbers.length; i++) 
    {
       if (myNumbers[i].substr(0,1) == "g") 
       {
          myNumbers[i] = myNumbers[i].substr(1,20);
          document.write("<font color = '#00FF00'>", myNumbers[i] + "</font> &nbsp&nbsp ");
       }
       else
       {
          document.write("<font color = '#FF0000'>", myNumbers[i] + "</font> &nbsp&nbsp ");
       }
    } 
}

// then call like this
checkArray(a1, b1);

Use arrays of arrays to store your numbers and the winning numbers

var a = [["2","3","8","12","23", "37", "41", "45", "48"],
         ["2","14","3","12","24", "37", "41", "46", "48"]];

var b = [["2","5", "11","16","23","45", "46"],
         ["1","23", "11","14","23","42", "46"]];

var Players = ["John", "Michael"];

//Now you can do this:
//  for each player
for(var k = 0; k < Players.length; k++)
{
    // compare his numbers with each draw
    for(var c = 0; c < b.length; c++)
    {
       checkArray(a[k], b[c]);
       displayNumbers(Players[k], a[k]);
    }
}

And adding a new player is as easy as

var a = [["2","3","8","12","23", "37", "41", "45", "48"],
         ["2","14","3","12","24", "37", "41", "46", "48"]];

var b = [["2","5", "11","16","23","45", "46"],
         ["1","23", "11","14","23","42", "46"]];

var Players = ["John", "Michael"];

//Add a new player:
Players[2] = "Adam";
a[2] = ["9","3","7","12","23", "37", "40", "45", "24"];

Of course you don't have to add people and draws at the same time..

Miky Dinescu
The comparison function is defined as `checkArray`, but called as `checkValues(a1, b1)`. Did you mean to write a `checkValues` function?
outis
yes, of course.. I fixed it
Miky Dinescu
A: 

I looks very nice thank you! I'm not sure if I did it correctly, but this is what I have. But it doesn't work in FireFox.

head section:

<script language="javascript">

// then call like this
checkValues(a1, b1);

var a = [["2","3","8","12","23", "37", "41", "45", "48"],
     ["2","14","3","12","24", "37", "41", "46", "48"]];

var b = [["2","5", "11","16","23","45", "46"],
     ["1","23", "11","14","23","42", "46"]];

var Players = ["John", "Michael"];

//Add a new player:
Players[2] = "Adam";
a[2] = ["9","3","7","12","23", "37", "40", "45", "24"];

function checkArray(myValues, winningValues)
{
for (var i = 0; i< myValues.length; i++) 
{
  for (var j = 0; j< winningValues.length; j++) 
  {
    if (myValues[i] == winningValues[j]) 
    {
      myValues[i] = "g"+ myValues[i];
    }
  }
}
}

 function displayNumbers(playerName, myNumbers)
{
document.write("<font color = '#FFFFFF'>" + "<b>" + playerName + " &nbsp&nbsp " + "</b></font>");
for (var i = 0; i< myNumbers.length; i++) 
{
   if (myNumbers[i].substr(0,1) == "g") 
   {
      myNumbers[i] = myNumbers[i].substr(1,20);
      document.write("<font color = '#00FF00'>", myNumbers[i] + "</font> &nbsp&nbsp ");
   }
   else
   {
      document.write("<font color = '#FF0000'>", myNumbers[i] + "</font> &nbsp&nbsp ");
    }
  } 
 }

</script>

body section:

<body onload="checkValues(a1, b1);">
Chris
Updating the details of your implementation should be edited into the original question rather than as new answers. Once you're at 50 rep, you can respond to other answers to ask questions by leaving a comment. Until then, please add the questions to your original one. It's just how SO is structured.
outis
@Chirs: please either change the call to checkValues to "checkArray" or change the name of the function to match ("checkValues"). In my example I made a mistake of naming the function checkArray and calling it with checkValues. @outis pointed that out and I've corrected my post. Please see below and correct your code as well..
Miky Dinescu
I've up-voted your question.. If you get involved and gain 30 more points you'll be able to post comments.
Miky Dinescu
I dont want to be rude, but I really don't know what to do next, I tried the snippets you provided, but I'm totally lost, can you help me back on track again?
Chris
+2  A: 

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.

var guesses = [["2","3","8","12","23", "37", "41", "45", "48"],
               ["2","14","3","12","24", "37", "41", "46", "48"]];
var draws = [ {2:1, 5:1, 11:1, 16:1, 23:1, 45:1, 46:1},
                {1:1, 23:1, 11:1, 14:1, 23:1, 42:1, 46:1}];

function checkArray(guesses, draw) {
    for (var i = 0; i< guesses.length; ++i) {
        if (draw[guesses[i]]) {
            guesses[i] = 'g' + guesses[i];
        }
    }
}
checkArray(guesses[0], draws[1]);

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.

function checkArray(guesses, draw) {
    var results = {}
    for (var i = 0; i< guesses.length; ++i) {
        if (draw.picks[guesses[i]]) {
            results[guesses[i]] = 'win';
        } else {
            results[guesses[i]] = 'loss';
        }
    }
    return results;
}
...
document.write('<span class="name">John</span>');
var results = checkArray(guesses[0], draws[1]);
for (var p in results) {
    document.write('<span class="'+results[i]+'">'+p+'</span>');
}

Since document.write is also deprecated, I'll replace it with the modern equivalents, document.createElement and Node.appendChild. If you think the resulting code is too verbose, you can instead use innerHTML, 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.

function Result(guesses) {
    for (var i = 0; i< guesses.length; ++i) {
        this[guesses[i]] = '';
    }
}
function checkDraw(guesses, draw, results) {
    for (var i = 0; i< guesses.length; ++i) {
        if (draw.picks[guesses[i]]) {
            results[guesses[i]] = 'picked';
        }
    }
    return results;
}

function appendTo(elt, parent) {
    if (parent) {
        document.getElementById(parent).appendChild(elt);
    } else {
        document.body.appendChild(elt);
    }
}

function printResults(name, results, parent) {
    var resultElt = document.createElement('div');
    resultElt.appendChild(document.createElement('span'));
    resultElt.firstChild.appendChild(document.createTextNode(name));
    resultElt.firstChild.className='name';
    var picks = document.createElement('ol');
    picks.className='picks';
    for (var p in results) {
        picks.appendChild(document.createElement('li'));
        picks.lastChild.appendChild(document.createTextNode(p));
        picks.lastChild.className = results[p];
    }
    resultElt.appendChild(picks);
    appendTo(resultElt, parent);
}

function printResultsFor(name, draws, parent) {
    var player = players[name];
    var results = new Result(player);
    for (var i=0; i<draws.length; ++i) {
        checkDraw(player, draws[i], results);
    }
    printResults(name, results, parent);
}

function printDraw(which, draw, parent) {
    var drawElt = document.createElement('div');
    drawElt.className='draw';
    drawElt.appendChild(document.createElement('h3'));
    drawElt.lastChild.appendChild(document.createTextNode('Draw '+which));
    drawElt.lastChild.className='drawNum';
    drawElt.appendChild(document.createElement('div'));
    drawElt.lastChild.className='date';
    drawElt.lastChild.appendChild(document.createTextNode(draw.when));
    var picks = document.createElement('ol');
    picks.className='picks';
    for (var p in draw.picks) {
        picks.appendChild(document.createElement('li'));
        picks.lastChild.appendChild(document.createTextNode(p));
    }
    drawElt.appendChild(picks);
    appendTo(drawElt, parent);
}

Here's the corresponding HTML page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html>
  <head>
    <style type="text/css">
      body {
        font-family: Verdana, Arial, Helvetica, sans-serif;
        color: white;
        background-color: #333;
      }
      .picks, .picks * {
        display: inline;
        margin: 0;
        padding: 0;
        list-style-type: none;
      }
      .picks * {
        margin: auto 0.25em;
      }
      #Results .picks * { color: red; }
      .name, .picks .name {
        color: white;
        font-weight: bold;
        margin-right: 0.5em;
      }
      #Results .picked { color: lime; }
      .drawNum, #Draws H3 {
          margin-bottom: 0;
      }
    </style>
    <script type="text/javascript" src="lotto.js"></script>
  </head>
  <body>
    <div id="Results"></div>
    <div id="Draws"></div>
    <script type="text/javascript">
    var players = {John:    [2,  3, 8, 12, 23, 37, 41, 45, 48],
                   Michael: [2, 14, 3, 12, 24, 37, 41, 46, 48]};

    var draws = [ {when: 'Sat 08-08-2009',
                   picks:{2:1, 5:1, 11:1, 16:1, 23:1, 45:1, 46:1}},
                  {when: 'Sat 15-08-2009',
                   picks:{1:1, 23:1, 11:1, 14:1, 23:1, 42:1, 46:1}}];

    for (name in players) {
      printResultsFor(name, draws, 'Results');
    }
    for (var i=0; i<draws.length; ++i) {
      printDraw(i+1, draws[i]);
    }
    </script>
  </body>
</html>

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.

outis
@ Outis, feel bored when you want, this is great, I'm not totally sure, but I think I saw a tiny bug. On draw 1 a 2 is picked but isn't green, is this normal, or do I mis something? Greate help this far!
Chris
@Chris: it's by design. I check each player's guesses against the current draw, rather than checking player N against draw N. I'm still not certain if the page is supposed to play a game and display the results (which is what your description seems to say and my code does) or display the winner's results for each game (which is what it looks like your original code was trying to do).
outis
@ outis The design was indeed check N against draw N, this was made by someone at codingforums, this isn't right. The code should be check all the numbers by all the draws, which mean that 5 people can pick "5" as a choosen number, but can be hit in draw week 3. In short, all the draw weeks should compared with all the players and checked (make it green) when a numbers is hit. It's not a game, but just a check form for a game we play at home. Normally we did it with a piece of paper with all the numbers on it, and we strike through those numbers(bingo). It can take weeks to complete the form
Chris
@ Outis @ when a person has all his numbers striked through (on paper) he wins the pot of gold :D I want this on a website so we don't get question for a new piece of paper becausethe dog eat the form or someone lost it.
Chris