What if the user answers inappropriate answer?


#1

What block of code should I put inorder to tell the program to stop and restart when the user answer an inappropriate answer?

I would like an explanation to help me understand how will it work and why.
thanks!

btw the "//1" and so on are my self not to tell myself if every { is closed.

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();

if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function (choice1, choice2) {//1
    
    if (choice1 === choice2) {//2
        return "The result is a tie!";
        
    }//2
    else if (choice1 === "rock") {//3
        if (choice2 === "scissors") {//4
            return "rock wins";
        }//4
    else    {//5
        return "paper wins"
            }//5
        }//3
    else if (choice1 === "paper") {//6
    if (choice2 === "rock") {//7
        return "paper wins";
        }//7
    else {//8
        return "scissors wins"
        }//8
    }//6

    else if (choice1 === "scissors") {//9
    if (choice2 === "rock") {//10
        return "rock wins";
    }//10
    else {//11
        return "scissors wins";
        }//11
    }//9

    }//1
   compare(userChoice, computerChoice);

#2

@enterylvl,

Maybe
https://discuss.codecademy.com/t/9-9-game-whats-wrong-with-my-while-loop/7519/2?u=leonhard.wettengmx.n


#3

The problem is that loops are not explained yet, i would use a do while loop:

do {
   var userChoice = prompt("Do you choose rock, paper or scissors?");
} while ( userChoice != "rock" && userChoice != "paper" && userChoice != "scissors")

in short, if the user enters something invalid, all conditions are true, true && true && true, which results in true, the loops keeps running. if the user enters something valid, the conditions are: true && false && true, which is false, the loops ends, the user enters something valid


#4

I'll agree with the earlier comment regarding using a loop in this lesson, perhaps we're encouraged to use the techniques we've been taught to this point. That said, in the example below, I did use the "or" symbol "||" and I don't believe they've been introduced yet either.

It seems that one could utilize another conditional IF statement within the compare function call to test if choice1 has expected values...


var compare = function(choice1, choice2) {
    if (choice1 === "rock" || choice1 === "paper" || choice1 === "scissors") {
        if(choice1 === choice2) {
            return "The result is a tie!";
        }
        else if (choice1 === "rock") {
            if (choice2 === "scissors") {
                return "rock wins";
            }
            else {
                return "paper wins";
                }
        }
        else if (choice1 === "paper") {
            if (choice2 === "rock") {
                return "paper wins";
            }
            else {
                return "scissors wins";
            }
        }
        else {
            if (choice2 === "rock") {
                return "rock wins";
            }
            else {
                return "scissors wins";
            }
        }
    }    
    else {
        return choice1 + " is not an option. Please try again and enter either 'rock', 'paper' or 'scissors'";
    }    
};

#5

Loops and the or operator or not introduced in this point of the course. I would be very impressed if you found a method to validate user input with what you learned in the lessons so far

Yes, we where running a bit to help with the validation of userinput


#6

i found this thread hoping to find an answer regarding the rock paper scissors inappropriate answer input.
if only i could figure a way out how to validate answers without using lessons that haven't taught yet i mean beyond the loops, etc.

i just need to know if it's possible so I wont stop figuring out for a solution. thanks!


#7

functions are taught, so you could do:

var validate = function(){
    var userChoice = prompt("Do you choose rock, paper or scissors?");
    if (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors"){
        validate()
    }
}
validate()

But as you can see, this code is more complicated then a loop. But the answer to your question is: Yes, it can be done with what you learned


#8

but the "!=" and the "&&" haven't even been taught yet :frowning:


#9

!== then? which is the same except it also checks data type. != is taught, && simply's means and. Really? I would have to check, but && is quit vital, i would say it is taught early in the course.

!== is taught, i checked it. Well, you could do something like:

var validate = function(){
    var userChoice = prompt("Do you choose rock, paper or scissors?");
    if (userChoice == "rock"){return}
    if (userChoice == "paper"){return}
    if (userChoice == "scissors"){return}
   validate()
}
validate()

There are so many ways to solve this problem.

Edit: This needs to be integrated into a existing rock, paper and scissors game.


#10

I came to this topic for the same reason as chesterkins2, and I wondered if you might tell me where you would put this block of code (your most recent one.) Would I put it in early on or add it on to the code I already have? Thank you!


#11

I am trying to help, but this means i am also trying to let you guys do the step of thinking (to improve understanding). Where would you put it? And why?

I would personally put it after the userChoice and ComputerChoice but before you start comparing with your compare function


#12

I understand, that is what I'm here for, learning coding, and this particular exercise is made so I can think. I was going to put it after the whole block that defines the code, and before the if else statements. it just seems like that would be a good place. I wasn't sure if it mattered where it went! :smile:


#13

Well, it does matter where you put it. Think about it for a second, what are the steps of the program? The steps are the following:

  • giving the computer a choice by means of random number
  • asking the user for input
  • compare the user and computer choice, and give a winner

Now, where would it make it sense to insert a user input validation?

i would put the user validation after you asked the user for input (since otherwise you don't have input yet, which makes validation rather difficult), but before you start comparing (you want to compare with valid user input)


#14

That makes sense, I should have thought about it a bit more! If I had, I would have realized that!:persevere: Thank you for your help.