Selection from Array

M

Mark Scott

Hi

I am trrying to write an application which allows you to select a car from
an array. I have the following code:

var carMake = ['Ford', 'Vauxhall', 'Renault', 'VolksWagen'];
var carModel = ['Focus', 'Astra', 'Laguna', 'Golf'];
var carIndex, input
input = window.prompt("Please Enter a car Make")
for (carIndex = 0; carIndex < carMake.length; carIndex = carIndex + 1)
{
if (input == carMake[carIndex])
{
document.write("You have chosen the "+carMake[carIndex]+" "+
carModel[carIndex])
}
}

If I enter a car make which is not in the array (ie BMW) how do I get the
program to continue prompting until I enter a legal make?
 
D

Doug Gunnoe

Hi

I am trrying to write an application which allows you to select a car from
an array.  I have the following code:

var carMake = ['Ford', 'Vauxhall', 'Renault', 'VolksWagen'];
var carModel = ['Focus', 'Astra', 'Laguna', 'Golf'];
var carIndex, input
input = window.prompt("Please Enter a car Make")
for (carIndex = 0; carIndex < carMake.length; carIndex = carIndex + 1)
{
 if (input == carMake[carIndex])
 {
 document.write("You have chosen the "+carMake[carIndex]+" "+
carModel[carIndex])
 }

}

If I enter a car make which is not in the array (ie BMW) how do I get the
program to continue prompting until I enter a legal make?

input = 'junk';

function testInput(){
if(input == 'Ford' || input == 'Vauxhall' || input == 'Renault' ||
input == 'VolksWagen')
return true;
else return false;
}

while(!(testInput())){
input = window.prompt("Please Enter a car Make");
}

Something like that.

A better idea IMO is to offer a drop down menu with only valid choices.
 
D

Doug Gunnoe

If you iterate through the carMake to get the index of carModel, I can
assume that you will have exactly 1 carMake element assigned to exactly
1 carModel element. Why not use an object then?

var Cars={
'Ford':'Focus',
'Vauxhall':'Astra',
'Renault':'Laguna',
'VolksWagen':'Golf'

}

var input;

while (!( input in Cars )) {
input = window.prompt("Please Enter a car manufacturer")

}

document.write("You have chosen the " + input + " " + Cars[ input ])- Hidequoted text -

- Show quoted text -

That's better.
 
D

Doug Gunnoe

That's better.- Hide quoted text -

Except the 'in' operator is 'Not supported in Internet Explorer 5.0
and below' according to Mozilla.

Meh, I still like it better.
 
T

Thomas 'PointedEars' Lahn

Doug said:
var carMake = ['Ford', 'Vauxhall', 'Renault', 'VolksWagen'];
[...]
If I enter a car make which is not in the array (ie BMW) how do I get the
program to continue prompting until I enter a legal make?

input = 'junk';

var input = "";
var rxMake = new RegExp("^(" + carMake.join("|") + ")$");
function testInput(){
if(input == 'Ford' || input == 'Vauxhall' || input == 'Renault' ||
input == 'VolksWagen')
return true;
else return false;
}

Superfluous anyway.
while(!(testInput())){
while(!rxMake.test(input))
{

input = window.prompt("Please Enter a car Make");
}

More indentation would have been nice.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,082
Messages
2,570,587
Members
47,209
Latest member
Ingeborg61

Latest Threads

Top