Rock, paper and scissors


#1

I dont know what's wrong with my codes because it's a message that says there result not is undefined

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

var compare = function(choice1, choice2)
{
if(choice1 === choice2) {
return "The result is a tie!";
}
else if(choice1 === "rock")
{
if(choice2 === "paper")
{
return "paper wins";
}
else {return "rock 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";}
}
};
compare(userChoice,computerChoice);


#2

your compare function is incorrect, i think it should look like this
//choice1 = userChoice choice2 = CompChoice
else if( userChoice === "rock" && computerChoice === "paper" ){
// do something
}

this way you could control the flow of the game, if there is more than one arguments or conditions
and when invoking: compare(userChoice, CompChoice);


#3

here is the complete code

var game = function(){
        debugger;
    var userChoice = prompt("pilih Rock, Paper atau Scisors?")
    var computerChoice = Math.random()
if (computerChoice < 0.34) {
    computerChoice = "Rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "Paper";
} else {
    computerChoice = "Scisors";

} 


function compare(userChoice, computerChoice){

    if (userChoice === computerChoice){
        console.log("its a tie")
    }
    else if (userChoice==="Rock" && computerChoice === "Scisors"){
       console.log("you choose rock and computer choose scisor result: rock win")   
    }

    else if (userChoice === "Paper" && computerChoice === "Rock"){
        console.log("you choose paper and computer choose rock result: rock win")
    }
    else if ( userChoice === "Scisors" && computerChoice === "Paper"){
        console.log("you choose scisors and computer choose paper result: scisors win")
}
// now define the computer when win here there is 3 more condition you must define
   else if (userChoice==="Rock" && computerChoice === "Paper"){
       console.log("you choose rock and computer choose Paper result: Paper win")   
    }

    else if (userChoice === "Paper" && computerChoice === "Scisors"){
        console.log("you choose paper and computer choose Scisors result: Scisors win")
    }
    else if ( userChoice === "Scisors" && computerChoice === "Rock"){
        console.log("you choose scisors and computer choose paper result: Rock win")
}

}
compare(userChoice, computerChoice);
}

game();

this game is not as simple as you think it contain closure concept (maybe)
you will learn closure concept somehow if you learn more