What if Choice1 is paper


#1

I am having trouble with "What if choice1 is paper"

this is the code I have currently
`/*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";
}
//paper

if (choice1===choice2) {
return "The result is a tie!";
}
else if(choice1 === "paper") {

if(choice2 === "rock")
return "paper wins";
}

else {
return 'scissors wins';
}
};`

It returns with "Your code returned 'paper wins' instead of 'scissors wins'". Any help? I'm new, so anything helps.

Thank you!
Owen


#2

You are missing a opening bracer here. This should be.

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

Then add a closing bracer after your first else.


#3

For no other reason than to toss in a rant...

Consider, for a moment, what value is there in blocking out our logic before cluttering it up with messy bits of code?

if () {

}
else if () {
    if () {

    } else {

    }
}
else if () {}
else if () {}

#5

Sorry?? I'm just trying to finish my game. I'm sorry if its "cluttered".


#6

That is not my point. It's about scaffolding first, like a sculptor, then fill in the details. If your code is not working then you are either not following your design, your design is flawed, or you do not have a design. Blocking helps to set up design constraints and control flow patterns. It really is a useful front-on approach. And we get to resolve our logic in the process.


#7

It just says that it returns "paper wins" but my else statement at the end returns with "scissors wins", so I really don't know why its saying this.


#8

Check all your brace placements. When we block things out first, this never becomes an issue once all the bits are filled in.


#9

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