PDA

View Full Version : extend a script with extra vars to check?



boemboem
08-14-2009, 12:04 PM
Hello all,

This is a code for checking numbers A1 -> B1, when there is a match, the code will be green, otherwise black.

Can somebody explain to me how I can extend this script, that more then one array is checked??


<script type = "text/javascript">

var a1 = ["2","5", "11","16","23","45", "46"];
var b1 = ["2","3","8","12","23", "37", "41", "45", "48"]


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< a1.length; i++) {
if (a1[i].substr(0,1) == "g") {
a1[i] = a1[i].substr(1,20);
document.write("<font color = 'green'>", a1[i] + " &nbsp&nbsp ");
}
else {
document.write("<font color = 'black'>", a1[i] + " &nbsp&nbsp ");
}
}
document.write("<br><br>");
for (var j = 0; j< b1.length; j++) {
document.write("<font color = 'black'>", b1[j] + " &nbsp&nbsp ");
}


</script>

Thanks a ton in advance

magicyte
08-14-2009, 05:34 PM
Yes, but the only logical and easy way to do this would be to make a larger array, one made of those smaller arrays you were talking about. This way, you can add more arrays to the larger array and have a function check the large array's length, afterwhich determine how many arrays there are and thus being able to add multiple arrays without modifying the current code. I do this a lot in JavaScript RPGs I sometimes make where I have a level map in a single array called "imageMap", then after that I can add more to the array and not have to modify the original code. Also, where the code is in text, you shouldn't have it like that. I recommend just using integers and not a string. Here is an example of what you could do:


<script type = "text/javascript">
// mdArray stands for "Multi-Dimensional Array"
var mdArray = [
["2", "5", "11", "16", "23", "45", "46"],
["2", "3", "8", "12", "23", "37", "41", "45", "48"],
["2", "6", "25", "32", "54", "9", "46"]
];

// I am not sure what you are trying to do, so I will not
// change this code. Now you understand what you can
// do with md arrays, and thus implement your
// knowledge on the current code
</script>

To access the first array (originally a1), you would type something like "mdArray[0]". To actually access the elements inside of that array, you would type "mdArray[0][0]". That would access the first number inside of the first array. I hope you understand. ;)

boemboem
08-15-2009, 09:40 AM
hello, thanks for your quick reply, O really hope you can help me further, because I'm struggeling with this for some time now. The MDarray looks good, but I think there will be a problem with what I want.

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 peoples 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 in in the link (http://www.coldcharlie.nl/lotto_test.html).

The code beneath works great, but the problem is that de 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];
}
}
}

// Joop
document.write("<font color = '#FFFFFF'>" + "<b>" + "Joop &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 ");
}
}

// Marijke
document.write("<br><br>");
document.write("<font color = '#FFFFFF'>" + "<b>" + "Marijke &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);

// Trekking

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>" + "Trekking 1 " + "</b>");
document.write("<br>");
document.write("<font color = '#FFFFFF'>" + "<b>" + "Zaterdag 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>" + "Trekking 2 " + "</b>");
document.write("<br>");
document.write("<font color = '#FFFFFF'>" + "<b>" + "Zaterdag 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>