Build "Rock, Paper, Scissors", it doesn't output winner


#1

So the program completed as a success and wants me to move on to the next chapter, but it only returns the computerChoice (Line 9), not the winner.

Here is my code:

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 if(choice1 === "scissors") {
if(choice2 === "rock"){
return "rock wins";
}
else {
return "scissors wins";
}
}
console.log(compare(userChoice, computerChoice))};


#2

check the last line
it should be
compare(userChoice,computerChoice)
and no "};" at the end.

Hope this helps.


#3

To be more clear here is the last lines of my code;

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


#4

It did, thank you!!

Now I need to study and understand why. Thank you again!


#5

I thought I'd need to do "console.log" to get it to return who wins. Why is that not the case?


#6

Function itself prints the result with "return" command but that was not the error I think,
The problem was you wrote the last command which prints the result inside the last "}" (it belogs to if statement) and when you take it out of brackets { } it worked. :wink:

As you can see there are 3 closing brackets "}" in my code and 2 in your code (last one is after the console.log)

I am also a begginer so this is what I see...


#7

Ah thank you. If I changed line 9 from console.log to document.write and did this inside an actual live HTML file, do you think that it would it work the same way?

Basically I want to show someone else my work.


#8

I have no idea about it, sorry :neutral_face:


#9

I think you can use labs repl.it to share and showcase your work.


#10

Thank you again. I really appreciate your help.