ROCK, PAPER, SCISSORS, PART 8 (Where's the bug?)


#23

It stops you from comparing. Why? else if choice2 === “rock” it suppose to return “rock wins!” And in action if it is really a choice 2(a rock) without even going to the next bit of code it will give you immediately “rock wins” as return always return you output and stops rest of the code to execute.

So, you might try the way the guideline in the exercise and my code:

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

Now, if choice1===“rock”, it will go to the next bit of code to see if choice 2 is scissors or not. This is how you give the code a chance to compare. By the way you cannot even use console.log() instead of return like console.log(rock wins); you need to nest if/else inside else/if. This exercise is to learn nesting too I guess.


#24

I don’t fully understand how I can inplement that code into the code i have already. How would I test for if choice1 is “scissors”?


#25

you need to write each of the three case of choice 2 (rock, paper, scissors). You have already written which is tie.

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

else if(choice2 === “paper”)
{
if(choice1 === “rock”)
return “paper wins”;
else
return “scissors win”;
}
can you do for:
else if(choice2 === “scissors”)
{
your code

}
can you see if/else are inside else if. This is nesting.


#26

You have to use compare(userChoice, computerChoice);

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”;
}

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 if(choice1 === “scissors”) {

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

}
compare(userChoice, computerChoice);


#27

var userChoice = prompt(“Do you choose rock, paper or scissors?”);
var computerChoice = Math.random();
if (computerChoice <= (1/3)) {
computerChoice = “rock”;
} else if (computerChoice <= (2/3)) {
computerChoice = “paper”;
} else {
computerChoice = “scissors”;
}
console.log("Computer: " + computerChoice);

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

compare(userChoice, computerChoice);