9/9: Build Rock, Paper, & Scissors


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661

How do I code to meet these requirements?

1.What if a user makes an inappropriate choice like 'dog'? How can we extend the function to handle that?

2.What if players in the game could also choose Rope in this game?
3. In this version, if both players make the same choice, the game returns a tie. What if the game didn't end there but instead asked both players for new choices?


Replace this line with your code. 
var Question1 = confirm("Let's play a game.");
    if (Question1 === "Ok") {
        console.log("Welcome to the game.");
        var choice1 = prompt("Do you choose rock, paper or scissors?");
    console.log("Player's choice: " + choice1);
var choice2 = Math.random();
if (choice2 < 0.34) {
	choice2 = "rock";
} else if(choice2 <= 0.67 && choice2 >= 0.35) {
	choice2 = "paper";
} else {
	choice2 = "scissors";
} console.log("Computer's choice: " + choice2);

var compare = function(choice1, choice2) {
    
    if(choice1 === choice2) {
    return "The result is a tie";
    }
    else if(choice1 === "rock") {

        if(choice2 === "scissors") {
            return "rock wins";
        }
        else { //paper
            return "paper wins";
        }
    }
    else if(choice1 === "paper") {
        
        if(choice2 === "scissors") {
            return "scissors win";
        }
        else { //rock
            return "paper wins";
        }
    }
    else if(choice1 === "scissors") {
        
        if(choice2 === "rock") {
            return "rock wins";
        }
        else { //paper
            return "scissors win";
        }
    }
};
    }
    else {
        console.log("I'm sorry to hear that. Perhaps, next time?");
    }


#2

this is a difficult one, since loops aren't covered yet. which would mean you need to use functions:

def valid(){
  var choice1 = prompt("Do you choose rock, paper or scissors?");
  if (choice1 == "rock"){
    return "rock";  
  } else if (choice1 == "paper"){
    return "paper";
  } else if (choice1 == "scissors"){
    return "scissors";  
  } else {
    return valid();
  }
}
var choice1 = valid();

nothing new here, except i call the function again when the choice isn't valid.

that shouldn't be to difficult?

simple wrap the game inside a function, if the result is a tie, call the function again.