Codecademy: JavaScript Build “Rock, Paper, Scissors” 9/9 – Next Steps

My Saturday nights are wild.

Below is my solution to Codecademy’s JavaScript 9/9. The three goals of the tasks were to:

  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?

I personally think this course isn’t written or structured well. The objective would not have been possible IMO with the given learning material. If it wasn’t due to my prior hair pulling experiences with Python and learning how to use Google to look up unfamiliar syntaxes, I wouldn’t have known any better because I think these objectives absolutely need while loops and many many more lines of codes. If I was a complete novice and approached that course (given that JavaScript is quite early on in Codeacademy’s material), I would have just gone to bed.

My solution below doesn’t even completely answer Step 3, but who cares. It’s Saturday night / Sunday morning.

Have a crack at it yourself, and let me know what you did for Step 3. I would be very interested to see a solution without a nested while loop – not that I used one, nor is there anything wrong with them,I just think they’re messy.

The objective I struggled with was having to evaluate the user’s entry when asked for an alternative choice in the event of a tie, whilst making sure I was still adhering to Step 1. My cheeky/lazy work around is line 32 which just denies anything else other than what’s suggested by the prompt, and it only asks 1 user for a new choice, not both.

var j = 0;
while (j < 1) {
    var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
    if (userChoice === "rock" || userChoice === "paper" || userChoice === "scissors" || userChoice === "rope") {
        j++;
    }   
    else {
        confirm("Please 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("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        var i = 0;
        while (i < 1) {
            choice1 = prompt("The result is a tie! Please choose again from rock, paper, scissors or rope: ");
            if (choice1 != choice2 && (choice1 === "rock" || choice1 === "paper" || choice1 === "scissors" || choice1 === "rope")) {
                i++
            }
            else {
                continue;
            }
        }
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "Rock wins!"
        }
        else if (choice2 === "rope") {
            return "Rock wins!"
        }
        else {
            return "Paper wins!"
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "Paper wins!"
        }
        else if (choice2 === "rope") {
            return "Paper wins!"
        }
        else {
            return "Scissors wins!"
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "Rock wins!"
        }
        else if (choice2 === "rope") {
            return "Scissors wins!"
        }
        else {
            return "Scissors wins!"
        }
    }
    else {
        if (choice2 === "rock") {
            return "Rock wins!"
        }
        else if (choice2 === "paper") {
            return "Paper wins!" 
        }
        else {
            return "Scissors wins!"
        }
    }
}

compare(userChoice, computerChoice);

Leave a Reply

Your email address will not be published. Required fields are marked *