I need to ask the user a prompt but they are only able to enter certain words for it to accept the answer, if the user does not answer with the right words it then say it is invalid and asks the question again.
This has to be done by only using javascript.
var entering = confirm('Confirm to add state or territory');
if (!entering) {
return;
} else {
while (true) {
stInput = prompt('Enter state or territory');
function validateUserChoice(stInput) {
if (stInput !== "Queensland" && stInput !== "New South Wales" && stInput !== "Victoria" && stInput !== "Tasmania" && stInput !== "Northern Territory" && stInput !== "Western Australia" && stInput !== "South Australia" && stInput !== "Australian Capital Territory") {
alert("invalid state or territory");
} else {
return false;
}
}
populationInput = parseInt(prompt('Enter population for ' + stInput + ''));
while (isNaN(populationInput)) {
alert('Your input was invalid');
populationInput = parseInt(prompt('Enter population for ' + stInput + ''));
}
changeInput = parseFloat(prompt('Enter growth rate for ' + populationInput + ''));
while (isNaN(changeInput)) {
alert('Your input was invalid');
changeInput = parseFloat(prompt('Enter growth rate for ' + populationInput + ''));
}
break;
}
}
This is what I have done so far but it is not validating so it I have done something wrong just not sure what.
The words the user can only answer with are: Queensland, New South Wales, Victoria, Tasmania, Northern Territory, Western Australia, South Australia, Australian Capital Territory.
wordsArray.indexOf(stInput) == -1, if true, means that the word is not in the array... It makes tour code cleaner and scalable