9/9 console.log(compare (userChoice, computerChoice));


#1


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


It is working, but I am not sure if I am writing codes correctly. Especially I am not sure about the way I use "console.log(compare (userChoice, computerChoice));" inside the function, and use "compare (userChoice, computerChoice);" outside the function.

Many thanks for your help,
Masa


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);

if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors"){
    
    console.log("Check your input");
};

var compare = function(choice1, choice2){
    if (choice1 === choice2){
        userChoice = prompt("Tie! Do you choose rock, paper or scissors?");
        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(compare (userChoice, computerChoice));
    }
    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"
        }
        
    }
    else if (choice1 === "rock"){
        
        if (choice2 === "paper"){
            return "paper wins"
        }
        else{
            return "rock wins"
        }
        
    };
};

compare (userChoice, computerChoice);


#2

I've been playing your game trying to trigger a tie between the user and computer. Something interesting is happening when there is a tie. Your code prompts a response from the user, the computer chooses and then it logs the result to the console. Sometimes the log shows the computerChoice and the resulting "rock/paper/scissors wins". Other times, undefined is logged to the console after the "rock/paper/scissors wins". I'm not sure why. Hopefully one of the mods can sort this out.


#3

Oh thanks for your response biff75. I have no idea why that is happening...!


#4

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