8. Don´t know what to do


#1

Here´s my code, but when I run it, it says "Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and rock"

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

};

#2

so essentially, this else statement is taking in all of instances that choice1 is scissors. In this case, your return statement is wrong, because paper will never win with scissors.
ignoring that logic, what you need to do is make sure you two conditionals, one that checks if choice2 is paper, and the other one checking if it's rock.
you would have to have a return statement accordingly.
does this make sense? do you have questions?


#3

Would that mean that I have to add more choice1´s and choice 2´s?


#4

what do you mean?
look at how you did it previously for rock:

else if (choice1=== "rock") {
    
if (choice2 === "scissors") {
        return"rock wins";
}
       
 else {
        return"paper wins";
        }
    }

now, the else if will be else (which will take all instances of choice1 being scissors), and you need to account for when choice2 is paper, and for when choice2 is rock, in relation to scissors with if and else statements inside of the main else statement.
am i making any sense? sorry if i'm not good at explaining!


#5

Ok, so I have to make the same program for each scenerio?


#6

kind of, but not exactly the same. the only two possibilities that are left are that while choice1 is scissors, choice2 is paper or rock. for each of these two, you need a return statement that says the outcome like, "scissors wins" or "rock wins"


#7

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