Trying to make a code where the user must input a string from a certain list to proceed. I am using a constant to list the valid strings however I need to be able to have two values be seen as equal.
const VALID_STATE = ('queensland%qld%new south wales%nsw%victoria%vic%northern territory%nt%western australia%wa%south australia%sa%tasmania%tas%australian capital territory%act');
How can I change this so that I can make QUEENSLAND and QLD be seen as the same value and therefore one is not valid if the other has already been entered. Below is the code I'm using to validate the user input.
state = prompt('Input a state name:');
while(VALID_STATE.indexOf(state.toLowerCase())<0){
alert('The input state was not valid.');
state = prompt('Re-enter the state name:');
Thanks in advance.
How about if someone enters "nd%ql"..? This is not the right way to do this job. Keep them in a Map object and map both "queensland" key and "qld" key to the same name of your choice.
var stateMap = new Map([['queensland','qld'], ['qld','qld'], ['new south wales', 'nsw'], ['nsw', 'nsw'], ['victoria', 'vic'], ['vic', 'vic'], ['northern territory', 'nt'], ['nt', 'nt'], ['western australia', 'wa'], ['wa', 'wa'], ['south australia', 'sa'], ['sa', 'sa'], ['tasmania', 'tas'], ['tas', 'tas'], ['australian capital territory', 'act'], ['act', 'act']]);
var state = prompt('Enter a state.');
while (!stateMap.has(state.toLowerCase())){
alert('The input state was not valid.');
state = prompt('Enter a state.');
}
state = stateMap.get(state);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With