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

simple array

Status
Not open for further replies.

kolbynash

Programmer
Apr 2, 2010
1
US
I am having a hard time figuring this out, I have two simple arrays and I want to populate one by asking a visitor to enter information, it goes something like this...
Code:
var country = new Array(5);
c_list[0] = "USA";
c_list[1] = "UK";
c_list[2] = "France";
c_list[3] = "Germany";
c_list[4] = "Spain";

var president = new Array(); // Last name only
president[0] = window.prompt("President of USA?", ""); // Obama
president[1] = window.prompt("Prime Minister of UK?", ""); // Brown
president[2] = window.prompt("President of France?", ""); // Sarkozy
president[3] = window.prompt("Prime Minister of Germany?", ""); // Merkel
president[4] = window.prompt("President of Spain?", ""); // Zapatero

So, I have the country array with assign numerically. Then I prompt for the president/prime minister of each country and those names are assigned to the president array so I can print (example) Obama is the President of USA. I am trying to figure out how to use a for loop to go through the prompts and assign the input to the president array, then I have to 'assign'(?) the now populated president array to the country array according to the numbers. Or rather I want to populate president by using a loop (with length property of the country array) in a for loop that prompts for the name of the president/prime minister.

Any help would be very kindly appreciated. I am just pulling at straws here.
 
Code:
var array = new Array();
array.push(1);
array.push('a');
array.push(new Date());
for(var i = 0; i < array.length; i++)
{
   console.log(array[i]);
}
there are probably better ways of solving this problem though. I would use an array of objects, rather than an array of primitive types related by index. this will make it easier to manage the information and separate the data from the presentation. for example:
Code:
var array = new Array();
array.push(new {
   country = 'USA',
   leader = 'Obama'
   office = 'President'
});
array.push(new {
   country = 'UK',
   leader = 'Brown'
   office = 'Prime Minister'
});

for(var i = 0; i < array.length; i++)
{
   var current = array[i];
   var question = 'Who is the '+current.office+' of '+current.country+'?';
   //append question to body
   //hide answer and compare with user input for correctness.
}


Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
@jmeckley
Parallel arrays can get disorganized very quickly. A better solution is to use associate arrays. This way you can change the order and not have to worry about maintaining more than one data structure

~Ron

Code:
<html>
<title>Test</title>
<head>
<script>
var oCountries = { "USA": {leader: "Obama", office: "President"},
		"UK": {leader:"Brown", office:"Prime Minister"}};

function quiz(){

	for(var sCountry in oCountries){
		var oCountry = oCountries[sCountry];
		var sAnswer = prompt("Who is the "+oCountry.office+" of "+sCountry+"?");
		if (sAnswer!=null && sAnswer.toLowerCase().indexOf(oCountry.leader.toLowerCase())!=-1)
			alert("You are correct!")
		else
			alert("You suck");
		
	}
	
}


</script>
</head>


<body>
<form>
	<input type="button" value="Quiz Me!" onclick="quiz()"/>
</form>


</body>


</html>

typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top