What if choice 1 is scissors?


#1


/*Why doesn't my "compare" function work? The code works, but doesn't log any of the function code.

Please help!

Thank you in advance! :) 
*/

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

if (computerChoice <= 0.33) {
   console.log("Computer chooses : rock")
}
else if (computerChoice > 0.34 && computerChoice < 0.66) {
    console.log("Computer chooses : paper")
}
else {
    console.log("Computer chooses : scissors")
}

console.log("You choose : " + userChoice)

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


#2

Your posted code runs:

http://jsbin.com/fepuhoyepu/edit?js,console

However, did you mean to add code for choice1==="scissors"? If so, that is missing. Also, please add a link to the exercise you're trying to solve.


#3

Thank you for the response.
The code I am trying to run is the "compare" function.
After I enter my answer via "prompt", shouldn't the "compare" function run and display one of the following?

Answer 1 : "The result is a tie!";
Answer 2 : "paper wins"
Answer 3 : "scissors wins"

Here is the link : https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661

Shouldn't the "compare" function print to the screen one of the following answers?


#4

Your posted code works as it shows in the jsbin link I posted. But you aren't following the instructions. Where are your code blocks for (choice1==="rock") and (choice1==="scissors")?


#5

Instructions are confusing, still trying to understand how IF, ELSE IF, ELSE works.

How do you add on to existing IF, ELSE IF, ELSE without syntax?

if (instructions) {
//code;
}
else if (instructions) {
//code;
}
else {
//code;
}


#6

In this code block, the if statement will log "The result is a tie!" to the console when choice1 exactly matches choice2. If choice1 and choice2 don't exactly match, the program moves on to the next line of code.

For this else if statement, when the choice1 input exactly matches "paper", your if/else statement tries to execute. If choice2 exactly matches "rock", "paper wins" logs to the console. If choice2 doesn't match "rock", the else statement logs "scissors wins" to the console.


#7

Try putting your compare(userChoice,computerChoice); outside of the very last closed curly brackets.

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice > 0 && computerChoice < 0.34) {
computerChoice = "rock";
}
else if(computerChoice > 0.34 && computerChoice < 0.66) {
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 if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}
}

};

compare(userChoice,computerChoice);


#8

This is what I was looking for, wasn't sure how ELSE IF continued after an IF statement. Thank you guys.


#9

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