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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to assign 2 or more values to variable in an array?

Status
Not open for further replies.

janicefoo

Programmer
Apr 17, 2003
53
0
0
MY
Dear gurus and members,
I am very new in javascript and programming. I would like to assign 3 words to a variable in an array. Is that possible? If yes, how can I do so?

The 3 words are "snooker", "pool" and "billiards". It is supposed to be assigned to arrAns[6] so that if user answers either one of the 3 words for question #6, the it will refer to correct answer for that question. However, I am not sure if is that possible to be done. I am at total lost now. :(

Looking forward to some replies soon.

Thanks in advance,
Janice
 
You probably want to use an array of arrays eg:
Code:
arrAns[6] = ["snooker","pool","billiards"];

You can then refer to either [tt]arrAns[6][/tt] to return the full array of correct answers, or (for example) [tt]arrAns[6][1][/tt] to return the second string out of the array, ie "pool".

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Thanks dwarfthrower,

So do you mean arrAns[6] has 3 strings? And within the arrAns[6] itself, there is another array (arrAns[6][0],arrAns[6][1], arrAns[6][2])? Is there anything I have to declare besides the following:

var arrAns = new Array(6);//array of the correct answers

Is the following code correct?

//Assign the correct answers to the array.
arrAns[0] = "baseball";
arrAns[1] = "badminton";
arrAns[2] = "volleyball";
arrAns[3] = "fencing";
arrAns[4] = "bowling";
arrAns[5] = "karate";
arrAns[6] = ["snooker","pool","billiards"];
arrAns[7] = ["sumo","sumo wrestling"];

Looking fotward to your reply.

Thanks in advance,
Janice
 
I'd do it more this way:
Code:
//Assign the correct answers to the array.
arrAns[0] = ["baseball"];
arrAns[1] = ["badminton"];
arrAns[2] = ["volleyball"];    
arrAns[3] = ["fencing"];
arrAns[4] = ["bowling"];
arrAns[5] = ["karate"];
arrAns[6] = ["snooker","pool","billiards"];
arrAns[7] = ["sumo","sumo wrestling"];

But an even simpler way (now that I have a better grasp _I think_ of what you're doing) would be to, instead of checking to see that the answer the user provided was equal to the answer in the array, check to see if the answer is contained in the string in the array. So instead of
Code:
var strResponse = prompt("What is the answer");
if(strResponse == arrAns[0][0]){
  alert("Correct");
}
else{
  alert("Wrong");
}

You could set up your array like this:
Code:
arrAns[0] = "~baseball~";
arrAns[1] = "~badminton~";
arrAns[2] = "~volleyball~";    
arrAns[3] = "~fencing"~;
arrAns[4] = "~bowling"~;
arrAns[5] = "~karate"~;
arrAns[6] = "~snooker~&~pool~&~billiards~";
arrAns[7] = "~sumo~&~sumo wrestling~";
And have your code more like:
Code:
var strResponse = prompt("What is the answer");
if(arrAns[0].indexOf("~" + strResponse + "~") >= 0){
  alert("Correct");
}
else{
  alert("Wrong");
}

Hope this gives you something to work with.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top