0

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.

3
  • Your code is almost correct but just that you cannot use return statements as and when you want. Instead of returning try to perform an operation or dont do anything at all. Commented Apr 26, 2019 at 12:27
  • off-topic tip: instead of checking every valid word, add all of then to an array, then check if the array contains the typed word, using wordsArray.indexOf(stInput) == -1, if true, means that the word is not in the array... It makes tour code cleaner and scalable Commented Apr 26, 2019 at 12:35
  • Possible duplicate of validating and looping javascript Commented Jun 18, 2019 at 11:31

3 Answers 3

2

When you write function validateUserChoice(stInput) you are declaring a function, not calling it. So you should whether declare your function and call it as validateUserChoice(stInput) or not use a function at all.

Following a snippet of code I believe you intended to do:

    var entering = confirm('Confirm to add state or territory');
    if (!entering) {
      return;
    } else {
      stInput = prompt('Enter state or territory');
      while (stInput !== "Queensland" && stInput !== "New South Wales" && stInput !== "Victoria" && stInput !== "Tasmania" && stInput !== "Northern Territory" && stInput !== "Western Australia" && stInput !== "South Australia" && stInput !== "Australian Capital Territory") {
        //stay in loop while stInput is not equal any of the strings
        alert("invalid state or territory");
        stInput = prompt('Enter state or territory');
      }
      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 + ''));
      }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

This is a perfect place to use do while loops. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
1

You're never actually calling the validateUserChoice(stInput) function. Here's an altered version of your code:

  var entering = confirm('Confirm to add state or territory');
  if (!entering) {
    return;
  } else {
      while (true) {
        stInput = prompt('Enter state or territory');

        // validate the state/territory and continue to prompt the user
        // until they enter correct data.
        while(!validateUserChoice(stInput)) {
            alert("invalid state or territory"); 
            stInput = prompt('Enter state or territory');
        }

        function validateUserChoice(stInput) {
            // put all valid entries into an array
            var validInputs = [
                "Queensland",
                "New South wales",
                "Victoria",
                "Tasmania",
                "Northern Territory",
                "Western Australia",
                "South Australia",
                "Australian Capital Territory"
            ]

            // if the user's input exists in the array the index will be > -1, otherwise the users
            // input is invalid.
            return validInputs.indexOf(stInput) > -1;
       }
        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;
      }

  }

Rather than checking against each string separately, I've clumped all possible state/ territory entries into an array, which allows you to simply check for the existence of the users input within that array. If it exists, the user's input is valid, otherwise it's invalid.

In addition, I added another "while" look that will continue to prompt the user for a valid state or territory until they provide one, much like you've done for your other entry points.

It's worth noting that this validation is case-sensitive. That is, "Queensland" is valid, but "queensland" is not. In addition, choosing "Cancel" in the prompt is also recognized as an invalid input. These scenarios should probably be handled.

1 Comment

Thanks for helping me out here. This is all helping me learn to be better at all of this.
0

Hope this is what you are looking,thanks.

function validate()
{
var entering = prompt("Confirm to add state or territory");
var populationInput='',changeInput='',stInputchangeInput='';
        if (entering) {
          stInput = prompt('Enter state or territory');
          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 {
                   
       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 + ''));
                }

           }
           console.log('State :'+stInput)
           console.log('Population :'+populationInput)
           console.log('Growth Rate :'+changeInput)
       }
     else if(entering !== null)
     validate()
}
validate()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.