What if choice1 is paper? I see nothing wrong with my code. please help!


#1

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

}
}
};

#2

You are closing your first else if at the end(after the second else if) you need to close it before the second else if


#3

Yea figured it out a few minutes ago. Thank you though! Can you help me with this other problem I'm having?


#5

paste in your code and I can have a look and try to help you


#6
var userChoice = prompt("Do you choose rock, paper,scissors or gun?");
var computerChoice = Math.random();
var ultimateChoice = prompt("Do you want to die?");
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else if(computerChoice <= 1) {
    computerChoice = "scissors";
} else {
    computerChoice = "gun"
} console.log("Computer: " + computerChoice);

var compare = function(userChoice, computerChoice, ultimateChoice) {
if(choice1 === choice2) {
    console.log ("The result is a tie");
}

else if(choice1 === "rock") {

    if(choice2 === "scissors") {
        console.log ("rock wins");
    }
    else {
        console.log ("paper wins");
    }

}
else if(choice1 === "paper") {
    
    if(choice2 === "rock") {
        console.log ("paper wins");
    }
    else {
        console.log ("scissors wins");
    }
}
else if(choice1 === "scissors") {
    if(choice2 === "paper") {
        console.log ("scissors wins");
    }
    else {
        console.log ("rock wins");
    }
}
else if(choice3 === "gun") {
    if(choice2 === "paper") {
        console.log("You're dead");
    }
}
};

It doesn't seem to print any of my console.log messages! and I want my ultimateChoice variable only to prompt when user inputs "gun"


#7

Works on my side and prints computer: rock

Try a refresh of your page


#8

No that it does. No problem there I mean what's the point of returning the "rock wins" etc. statements if they never appear anywhere?


#9

The function never gets executed, since there is no function call. So, the none of the console.log statements gets executed, so nothing is shown.

Add a function call, pretty sure you are going to add it the next lesson, or the lesson after the next lesson.


#10

You mean I have to define a function that checks for win statements? Or? An example block of code would be helpful


#11

You have the function, but you never call it. A function is just block of code, which gets executed when you do a function call. Why run ahead of the lessons? You will add a function call shortly

Or not, well, in that case, you learned how to make a function call in this lesson


#12

Okay so I'm just brainstorming here. but will this work.

under the if statements.
console.log(userChoice + "wins")


#13

i hadn't seen you customize your code this much, and customize it this poorly. Well, since userChoice is one of the parameters in your function:

var compare = function(userChoice, computerChoice, ultimateChoice)

console.log(userChoice + "wins") will work, but all your comparison not. In the lesson, you pass choice1 and choice2 in the function:

var compare = function(choice1,choice2)

which is why you do all the comparisons with choice1 and choice2. You might want to learn more about functions before customizing it in such a way.


#14

Okay thanks. Do functions come again in later modules?


#15

You already had them? Yea, they will come back, but just to use them, little explanation, you might want to go over the function course again


#16

Haha okay will do a quick refresher to be able to customise my rock paper scissors game.


#17

2 posts were split to a new topic: What if choice1 is paper?


#19