![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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? |
#3
| |||
| |||
|
|
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? |
#4
| |||
| |||
|
|
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 - |
#5
| |||
| |||
|
|
That's better.- Hide quoted text - |
#6
| ||||
| ||||
|
|
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'; |
|
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"); } |
#7
| |||
| |||
|
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
| |