Advance options restarting the game on match or invalid value


#1


Greetings, I am brand new to javascript and have no real world experience in coding. I am trying to run through these exercises so I can start using javascript. I am on the last exercise and want to finish it off but I am having issues.


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


It does not print tie before prompting user for input again. I want it to tell the user it was a tie and then the game basically resets... I also want to do this for an invalid value


//rock paper scissors game in Javascript

//user choose rock paper or scissors
var userChoice = prompt("Do you choose rock, paper or scissors?");

//computer randomly chooses between rock paper and scissors
var computerChoice = Math.random();

//if statement to determine computer's choice
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
        computerChoice = "paper";
    } else {
        computerChoie = "scissors";
    }



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 {
            return ("paper wins");
        }
    }
    else if (choice1 === "paper") {
        
        if (choice2 === "rock") {
            return ("paper wins");
        } 
        else {
            return ("scissors wins");
        }
    }  
        else if (choice1 === "scissors") {
        
        if (choice2 === "rock") {
            return ("rock wins");
        } 
        else {
            return ("scissors wins");
        }
    } 
    
};

//print computer choice
console.log("Computer: " + computerChoice);

//print user choice
console.log("You: " + userChoice);

//compare and declare winner
var results = compare(userChoice, computerChoice);
console.log (results);

//restart game if tie
if (results === "The result is a tie!") {
    var userChoice = prompt("Do you choose rock, paper or scissors?");
}


#2

When you return a value, it doesn't print it - it only assigns it to the variable you chose (in this case, the variable is results). If you want to print it you'll need to do console.log(results); before that last if statement.

Also, it's best to put the choices code in a function so you can call it to restart the game.

Hope this helps :slight_smile:


#3

I'm confused...I do have the console.log(results); prior to the last if statement. can you please explain what you mean?

also how would I write those as functions? and when would I call them? Like I mentioned, brand new and very green.

var userChoice = function(?){
prompt("Do you choose rock, paper or scissors?");
}
var computerChoice = function (?) {
Math.random();
}

//compare and declare winner
var results = compare(userChoice, computerChoice);
console.log (results);

//restart game if tie
if (results === "The result is a tie!") {
    var userChoice = prompt("Do you choose rock, paper or scissors?");
}

#4

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