Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing Array Values, and checking for that value

Status
Not open for further replies.

JSNewb

Technical User
Feb 7, 2006
6
US
Ok I am building a script that takes a list of teams, selects 1 of them, and puts them into a group. Then it goes back, selects another randomly and puts them in another group.

That works great, except of course I get duplicates. So my thought in checking for duplicates, was to first replace the selected team with a new value ("null"). Then I could just check for the value "null" each pass, and if it was found, redo the selection.

Problem is I am having trouble getting it to replace values and stay that way for every pass. Here is the simple selection code im using...

function getTeamA() {
var whichTeam=get_random();

var team=new Array(7)
team[0]="Elks";
team[1]="Dogs";
team[2]="Cats";
team[3]="Tigers";
team[4]="Bulls";
team[5]="Sharks";
team[6]="Jets";
team[7]="Sheep";

pickA = team[whichTeam];
}

I thought of using splice() or just using something like

newValue = "null"
team[whichTeam] = newValue

But when I do either, it either doesnt work at all, or it displays "null" on the line with the original value, instead of replacing. So I can't even get to the next step of checking for it.

Any Help?
 
I may be wrong but I think you are not able to set a null value into an array. You can set it empty like this:
team[whichTeam]=""; but you cannot set it to null.

To use splice you do it like this:
team[whichTeam].splice(x, y);
x = the position in the array to delete.
y = how many elements to delete.
So:
team[whichTeam].splice(3, 2);
would remove the fourth and fith elements in the array because array's start counting at 0.
team[whichTeam].splice(2, 1);
would remove the third array element only.


Stamp out, eliminate and abolish redundancy!
 
That's because you're using the word "null" and not the value null, and your array is local, not global. You could also have the function like this:

Code:
var team=new Array("Elks", "Dogs", "Cats", "Tigers", "Bulls", "Sharks", "Jets", "Sheep");

function getTeamA()
{
do
  {
  var whichTeam=get_random();

  pickA = team[whichTeam];
  team[whichTeam] = null;
  }
while (pickA == null);

}
Lee
 
Thanks for the advice guys. I got it to work now. However I have a couple bugs I can't seem to work out. As my name implies im a newb when it comes to JS. I am teaching myself, just by doing it and reading some.

That said, my code is prolly much longer and archaic then it needs to be. But thats how we learn right?

Here is the link to the as of now finished code.

It takes a predetermined 48 teams, set in bands of 8, and draws 1 name from each of the bands to make a group. It continues this pattern till all teams are selected, and the 8 exceptions are avoided (teams that cannot be paired).

The Bugs:
1) Sometimes when it runs, it will error out with "whichTeam is not defined." I tried to avoid that by making it a universal variable, but it still happens.

2) When you hit the "draw teams" button more then once, it of course locks up the browser. I tried to trap that problem by setting a "tick" value after the end of the first run. Then check for that value and show an alert saying you need to refresh the screen to try again. Sometimes it works, sometimes not.

3) My reset button doesn't always seem to reset the page the same way a "refresh" would. It blanks out the page, but when you hit draw team it sometimes acts like the draw is done, and locks up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top