Build "Rock, Paper, Scissors


#1


What if choice1 is scissors?

I'm receiving this message "Oops, try again. Your code returned 'The result is a tie!' instead of 'undefined' when the inputs are scissors and rock"" I would also be grateful, if someone tells me, what else is wrong with my code.


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)
{
    var choice1 = userChoice;
    var choice2 = computerChoice;
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 === 'paper') {
        return 'scissors wins';
    }
    else {
        return 'rock wins';
    }
}
}


#2

Remove both lines. choiceN are locally defined variables to receive the arguments in function call...

compare(userChoice, computerChoice);

#3

Thank you, but why when i run the code and write rock, paper or scissors, the program prints out "Computer:paper" for example. Shouldn't it return "paper wins"?


#4

Yeah. Got the same problem. Tried changing "return" to "console.log" but that didn't work.


#5

The return and console.log statements do different things. Return is only used inside functions and does not log anything to the console. Console.log is used to display something to the console. You can see what your function is doing by inputting a function call, along with the function arguments(if any) to console.log.

Ex:

console.log(function(argument1, argument2...));


#6

Yes, it should return "paper wins"; according to the instructions.


#7

Thanks, your advice was very useful.


#8

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