9/9 Solution: Can somebody please review my code/version of the game?


#1

The objective was to modify the game to execute all three suggestions as follows:

  1. Extend the function to handle when a user enters an inappropriate choice;
  2. Introduce a fourth choice to the game: rope;
  3. Ask players to pick another choice when both previously made the same choice.

Here is my code:

var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
var computerChoice = Math.random();
if (computerChoice <= 0.25) {
	computerChoice = "rock";
} else if(computerChoice <= 0.50) {
	computerChoice = "paper";
} else if(computerChoice <= 0.75) {
    computerChoice = "scissors";
} else {
	computerChoice = "rope";
} 
console.log("You choose " + userChoice);
console.log("Computer chooses " + computerChoice);

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        prompt("You both picked the same choice. Pick again: do you choose rock, paper, scissors or rope?");
    }
    else if(choice1 === "rock") {
        if(choice2 === "rope") {
            return "Rock wins against rope: You win!";
        }
        else if(choice2 === "paper") {
            return "Rock loses to paper: Computer wins!";
        }
        else {
            return "Rock vs scissors: It's a draw!";
        }
    }
    else if(choice1 === "paper") {
        if(choice2 === "rock") {
            return "Paper wins against rock: You win!";
        }
        else if(choice2 === "scissors") {
            return "Paper loses to scissors: Computer wins!";
        }
        else {
            return "Paper vs rope: It's a draw!";
        }
    }
    else if(choice1 === "scissors") {
        if(choice2 === "paper") {
            return "Scissors wins against paper: You win!";
        }
        else if(choice2 === "rope") {
            return "Scissors loses to rope: Computer wins!";
        }
        else {
            return "Scissors vs rock: It's a draw!";
        }
    }
    else if(choice1 === "rope") {
        if(choice2 === "scissors") {
            return "Rope wins against scissors: You win!";
        }
        else if(choice2 === "rock") {
            return "Rope loses to rock: Computer wins!";
        }
        else {
            return "Rope vs paper: It's a draw!";
        }
    }
    else {
        return "You entered an inappropriate choice. Pick only one from the following: rock, paper, scissors, rope.";
    }
}

compare(userChoice, computerChoice);

I had devised the game to include three possible outcomes for each combination (aside from the prompt when both players pick the same choice): win, lose or draw. For example, paper wins against rock, loses to scissors, and ties with rope (pun intended). Some combinations may not make sense logically but they're made that way so that all outcomes are fair.

My only problem is that when both players make the same choice, a prompt asks the user to enter another choice but the game stops there. It doesn't run the compare function again. I don't know how to fix this as I don't remember this being introduced in the course so far.

Anyway that's it! As far as I've tested the code works fine in all aspects and executes the three suggestions properly. I just wanted to share my solution and ask for reviews. Had I missed anything? Is there anything I could improve on (syntax, etc)? Comments are appreciated.


#2

Well actually this exercise has some conceptual problems. First of all the additional weapon according to wikipedia it makes more sense to have an odd number of options like the classical rock,paper,scissors, lizard, spock:


So your version with rock is pretty good for the circumstances. The prompt in case of tie is on the other hand not a good idea, mainly because it is never used again. You just prompt the user for an input but that input stays in the function without being used in any way. The inappropriate choice solution is again pretty good.
The problem with running the code again is that loops haven't been introduced so far which would make it a lot easier, afaik this was part of an older track where it was located after some basic introduction to loops. So if you like go on and revisit it when you learned about loops.
What you could do as well so far is to just use an if and see it run again 1 time.

One last hint: Try not to solve the looping part inside the function like you did with the prompt rather let the function just determine who won but use its output e.g.

...
var result = compare(...,...);
if(result === "tie message){
   let computer and player choose again
   compare(newValueA, newValueB);
}