Spicing up the Rock,Paper, Scissors questions


#1


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


I am trying to make a Tie result prompt the question for another try at the game. Also I am trying to make the last else statement force the user to say rock,paper, or scissors. the problem i'm having is it seems to get stuck on storing only the first input i give it for the userChoice variable. any hints or suggestions would be much appreciated! :slight_smile: thank you!


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);
console.log("You picked: " + userChoice);

var compare = function(choice1,choice2){
    if(choice1 === choice2){
        confirm("Tie! Pick again.");
        userChoice = prompt("Do you choose rock, paper or scissors?");
        computerChoice = Math.random();
        compare(tieuserChoice,tiecomputerChoice);
    } else if(choice1 === "rock"){
        if(choice2 === "scissors"){
            return "You win with " + choice1 + "!";    
        } else {
            return "Computer wins with Paper!";
        }
    } else if(choice1 === "paper"){
        if(choice2 === "rock"){
            return "You win with " + choice1 + "!";
        } else {
            return "Computer wins with scissors!";
        }
    } else if(choice1 === "scissors"){
        if(choice2 === "paper"){
            return "You win with " + choice1 + "!";
        } else{
            return "Computer wins with rock!";
        }
    }else {
        confirm("You did not pick rock, paper or scissors!");
        userChoice = prompt("Do you choose rock, paper or scissors?");
        computerChoice = Math.random();
        compare(userChoice,computerChoice);
    }
};

compare(userChoice,computerChoice);


#2

Hello :slight_smile:

In this part of code:

if(choice1 === choice2){
  confirm("Tie! Pick again.");
  userChoice = prompt("Do you choose rock, paper or scissors?");
  computerChoice = Math.random();
  compare(tieuserChoice,tiecomputerChoice);
}

please note that computerChoice must have a "rock", "paper" or "scissors" value based on the result of Math.random(). Just like in the beginning of the script.

From the function call I see that you have planned to use different names this time - tieuserChoice and tiecomputerChoice, there is no need to do that :slight_smile:


In the else statement you also have to assign to computerChoice value of "rock", "paper" or "scissors".

Also I am trying to make the last else statement force the user to say rock,paper, or scissors

To do so you can use a do while loop and indexOf method:

do {
  confirm("You did not pick rock, paper or scissors!");
  userChoice = prompt("Do you choose rock, paper or scissors?");
} while (["rock", "paper", "scissors"].indexOf(userChoice) === -1);

But here is a question - is it smart to do this in the else statement? Why not in the beginning of the script?


#3

you can do the test in the being it would make it easier to debug later on . also, could you have used the or statement to build the exit condition of the do loop.


#4

also, could you have used the or statement to build the exit condition of the do loop.

Yes, we can use || to do that:

userChoice === "rock" || userChoice === "paper" || userChoice === "scissors"

Now add a "spock" and "lizard" options and you have a really long condition.


#5

Ok ok hmmm. Awesome. How do I go about showing the info in html site.
Because i tried document.wrtie and I get the choices for the computer and
player to show but when I try to show the results from the compare function
it does not show. Any tips?

Sent using CloudMagic Email
[https://cloudmagic.com/k/d/mailapp?ct=pi&cv=7.4.15&pv=9.1&source=email_footer_2]


#6

You have to create a HTML container with assigned id and then, in JavaScript code, update property innerHTML of this element. Example:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Output test</title>
</head>

<body>
  <div id="output"></div>

  <script>
    // select our element with id output
    var output = document.getElementById('output');

    for (var i = 0; i < 10; i++) {
      // add some content to our element
      output.innerHTML += '<p>' + i + '</p>';
    }
  </script>
</body>

</html>

#7

ok I got it thanks I play around and see if I can get it to show up.


#8

You're welcome! In case of any problems do not hesitate to ask :slight_smile:


#9

Thanks so much for the response! I am eager to try out what you have said!


#10

You're very welcome :slight_smile:


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.