HighDots Forums  

Selection from Array

Javascript JavaScript language (comp.lang.javascript)


Discuss Selection from Array in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mark Scott
 
Posts: n/a

Default Selection from Array - 01-28-2008 , 03:28 PM






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?


Reply With Quote
  #2  
Old   
Doug Gunnoe
 
Posts: n/a

Default Re: Selection from Array - 01-28-2008 , 03:56 PM






On Jan 28, 2:28*pm, "Mark Scott" <mark-sc... (AT) blueyonder (DOT) co.uk> wrote:
Quote:
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.


Reply With Quote
  #3  
Old   
Karol Kowcik
 
Posts: n/a

Default Re: Selection from Array - 01-28-2008 , 04:17 PM



Mark Scott wrote:
Quote:
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?
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 ])


Reply With Quote
  #4  
Old   
Doug Gunnoe
 
Posts: n/a

Default Re: Selection from Array - 01-28-2008 , 04:19 PM



On Jan 28, 3:17*pm, Karol Kowcik <kow_usun_... (AT) gmail (DOT) com> wrote:

Quote:
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.


Reply With Quote
  #5  
Old   
Doug Gunnoe
 
Posts: n/a

Default Re: Selection from Array - 01-28-2008 , 04:34 PM



On Jan 28, 3:19*pm, I <douggun... (AT) gmail (DOT) com> wrote:

Quote:
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.




Reply With Quote
  #6  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Selection from Array - 01-28-2008 , 05:06 PM



Doug Gunnoe wrote:
Quote:
On Jan 28, 2:28 pm, "Mark Scott" <mark-sc... (AT) blueyonder (DOT) co.uk> wrote:
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("|") + ")$");

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

Quote:
while(!(testInput())){
while(!rxMake.test(input))
{

Quote:
input = window.prompt("Please Enter a car Make");
}
More indentation would have been nice.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee


Reply With Quote
  #7  
Old   
Michael White
 
Posts: n/a

Default Re: Selection from Array - 01-29-2008 , 09:59 AM



Doug Gunnoe wrote:

Quote:
On Jan 28, 3:19 pm, I <douggun... (AT) gmail (DOT) com> wrote:


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.


Could be a problem for a bad speller, and quite annoying, too.
Mick


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.