8. What if choice1 is scissors? <Oops, try again. Your code returned 'rock wins' instead of 'undefined' when the inputs are scissors and paper>


#1

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

//-----------------------------

if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} 

//-----------------------------

var compare = function(choice1, choice2){
    if(choice1 === choice2){
        return "The result is a tie!";        
    }
    
    if(choice1 === "rock"){
        if(choice2 === "scissors"){
            return "rock wins";
        }else{
            return "papper wins";    
        }    
    }
    
    if(choice1 === "papper"){
        if(choice2 === "rock"){
            return "papper wins";
        }else{
            return "scissors wins";    
        }    
    }
    
    if(choice1 === "scissors"){
        if(choice2 === "papper"){
            return "scissors wins";
        }else{
            return "rock wins";    
        }    
    }
};
    
console.log(compare(userChoice, computerChoice));

#2

here:

if(choice2 === "papper")

you misspelled paper, so it will return rock wins when computer picks paper and user picks scissors


#3

Oh my god, thanks. I'm like i did everything right, why is it not working. i will spell check next time. thanks again.


#4

you so nearly did :slight_smile:

The devil is in the details as they say


#5

@usuf94, on the side note,

your code indentation is beautiful and nicely kept. So pleasing to the eyes to read. Sorry for the diverted reply. Just want to mention my appreciation for that.

:wink:


#6

Thanks, I try my best to make it look neat. It makes the whole coding thing easier.


#7

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