skip to Main Content
 function getHumanChoice() {
      
      let humanChoice = prompt('Choose rock, paper, or scissors:');
      
      if (humanChoice.toLowerCase() !== humanChoice){
        humanChoice = humanChoice.toLowerCase()
      }


      while (humanChoice !== 'rock' && humanChoice !== 'paper' && humanChoice !== 'scissors') {
        alert('Please enter a valid choice');
        humanChoice = prompt('Choose rock, paper, or scissors:');
      }
      
      return humanChoice
    }


I get a typeError in the console whenever I try to close the prompt.

since the error is caused by executing toLowerCase() to null i tried to solve it by an if statement, i got rid of the error but the prompt keeps showing up and console doesnt do anything anymore.

if(humanChoice === null){
        return null
} else {
        if (humanChoice.toLowerCase() !== humanChoice){
          humanChoice = humanChoice.toLowerCase()
        }


        while (humanChoice !== 'rock' && humanChoice !== 'paper' && humanChoice !== 'scissors') {
          alert('Please enter a valid choice');
          humanChoice = prompt('Choose rock, paper, or scissors:');
        }
        
        return humanChoice
}

2

Answers


  1. You can keep everything in a functionWe need to breack the loop, not by the promp exit button but by using breack;. We have to do this becuase are loop is forever unless we change it.

     function getHumanChoice() {
          
          let humanChoice = prompt('Choose rock, paper, or scissors:');
          
          if (humanChoice.toLowerCase() !== humanChoice){
            humanChoice = humanChoice.toLowerCase()
          }
    
    
          while (humanChoice !== 'rock' && humanChoice !== 'paper' && humanChoice !== 'scissors') {
            //We use going as continue is a keyword
            let going = window.prompt("Invailid answer. Would you like to continue? Y/N")
             //Seeing if user wants to continue, if not breack the loop.
            if(going == "Y"){
                alert('Please enter a valid choice');
                humanChoice = prompt('Choose rock, paper, or scissors:');
            } else { //If user types "N" for no breack and stop the loop.
                alert("Thanks for playing!")
              break;
            }
          }
          
          return humanChoice
        }
    
    getHumanChoice() //Running code.
    

    Also using your suggested solution would not work as the if statment is not in our loop plus we do not use .toString to get our true undiefind value.

    Login or Signup to reply.
  2. Instead of using truthy/falsey directly in the while loop, use this:

    // initialize the variable to hold the function.
    let getHumanChoice;
    
    getHumanChoice = function (wasEmpty = !1) {
      if (wasEmpty) {alert("Sorry, that was empty or invalid!")}
    
      // first, initialize the variables in the local space.
      let answer, answered;
      answered = !1; // !1 = false
          
      // loop, only if nothing is in the string
      while (!answered) {
        answer = prompt("Please pick: rock, paper, or scissors!", '');
        if (answer !== '') {answered = !0; /*!0 = true*/}
      }
          
      // use regular expressions to capture the three choices and test is the result ISN'T one of the three.
      if (!(/rock|paper|scissors/i).test(answer)) {return getHumanChoice(!0)}
          
      return answer;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search