9/9 options 1 & 2 | Modifying Rock, paper, scissors [JavaScript]


#1



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

Hello, I've been fiddling around with this. I figured out a way to use rope as an option and how to deny options aside from rock, paper, scissors and rope. Can someone help me figure out how to give the player an option to play again if there is a tie? As you can see below, a tie in this game ends it.


var userChoice = prompt("Do you choose rock, paper, rope or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.17) {
	computerChoice = "rock";
} else if (computerChoice < 0.34) {
	computerChoice = "paper";
} else if (computerChoice < 0.67) {
	computerChoice = "scissors";
} else {
    computerChoice = "rope";
}
console.log("User: " + userChoice);
console.log("Computer: " + computerChoice);
var compare = function(userChoice, computerChoice) {
    if (userChoice === computerChoice) {
        return "we have a tie. no winner.";
    } else if (computerChoice === "rope") {
        return "computerChoice is rope, user wins";
    } else if (userChoice === "rock") {
        if (computerChoice === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    } else if (userChoice === "paper") {
        if (computerChoice === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    } else if (userChoice === "scissors") {
        if (computerChoice === "rock") {
            return "rock wins";
        } else {
            return "scissors wins";
        }
    } else if (userChoice === "rope") {
        return "you don't know how to play paper, rock, scissors. default win for computer.";
    } else if (userChoice != "rock" || "paper" || "scissors" || "rope"){
        return "invalid selection, you forfeit, computer wins";
      }
    };
console.log(compare(userChoice, computerChoice));


#2

To do that I suggest creating a new function that evaluates for a tie and then prints out the userChoice variable that prompts the user for rock, paper, or scissors. So if the result is a tie, you would print userChoice. And Your if statement that checks for a tie should be in your new play again function not the compare function.


#3

first off, your comparison here:

else if (userChoice != "rock" || "paper" || "scissors" || "rope"){

is bogus, it won't work. There is even a topic for multiple comparsions

now, the problem is that a do while loop is ideal, but this hasn't be covered yet. What you can do is a function:

var valid_choice = function(){
    var userChoice = prompt("Do you choose rock, paper, rope or scissors?");
    if (condition){
      // the user entered something valid, return the userChoice
      return userChoice;
   }
   // the user didn't enter something valid, call the function again:
   return valid_choice();
}
userChoice = valid_choice();

something like this. The condition of the if statement is still for you t to determine


#4

You could also add an informative message there like this :smiley:

var valid_choice = function(){
    var userChoice = prompt("Do you choose rock, paper, rope or scissors?");
    if (condition){
      // the user entered something valid, return the userChoice
      return userChoice;
   }

    alert("Alas! The choice you entered wasn't proper. Try again!")
   // the user didn't enter something valid, call the function again:
   return valid_choice();
}
userChoice = valid_choice();

Question for OP: Do you think the alert message would work as intended if I put it right at the first line? Immediately before/after the prompt?


#5

I don't think it would run before the first line because userChoice has to be defined first.


#6

if (userChoice != "rock" ||userChoice != "paper" || userChoice !="scissors" || userChoice != "rope"){

^^^
Would the bogus line work if I substituted this?


#7

now you have valid syntax, but it will not validate correctly. To make it yourself a whole lot easier, i would simply use == instead of !=.


#8

var valid_choice = function(){
var userChoice = prompt("Do you choose rock, paper, rope or scissors?");
if (userChoice === "rock"||userChoice === "paper"||userChoice === "scissors"||userChoice === "rope"){
// the user entered something valid, return the userChoice
return userChoice;
}
// the user didn't enter something valid, call the function again:
return valid_choice();
}
userChoice = valid_choice();
var computerChoice = Math.random();
if (computerChoice < 0.17) {
computerChoice = "rock";
} else if (computerChoice < 0.34) {
computerChoice = "paper";
} else if (computerChoice < 0.67) {
computerChoice = "scissors";
} else {
computerChoice = "rope";
}
console.log("User: " + userChoice);
console.log("Computer: " + computerChoice);
var compare = function(userChoice, computerChoice) {
if (computerChoice === "rope") {
return "computerChoice is rope, user wins";
} else if (userChoice === "rock") {
if (computerChoice === "scissors") {
return "rock wins";
} else {
return "paper wins";
}
} else if (userChoice === "paper") {
if (computerChoice === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
} else if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "rock wins";
} else {
return "scissors wins";
}
} else {
return "you don't know how to play paper, rock, scissors. default win for computer.";
}
};
console.log(compare(userChoice, computerChoice));

^^^^^
that's my new code, thanks for all the help @bandit @gaurangtandon @stetim94 :slight_smile: