Build "Rock, Paper, Scissors" What if choice1 is scissors?


#1

I can't seem to find the problem with my code? I've rewritten it but still can't figure it out. Any help would be massively appreciated.

I got the error: SyntaxError: missing before statement.

Also if you can, let me know if there's any way I can improve the general quality of the code below.

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

*compare(userChoice, computerChoice);*

Thanks,
Finn


#2

at the end dont put a semi colon here -------> ;}

also u need to do this ---> console.log (compare (userChoice, computerChoice));

instead of this ---> compare(userChoice, computerChoice);

in addition u need to remove all the asterixs


#3

Hi,

copied and pasted below code so you can have a gander and work out where tweaks could be incorporated to improve your code:

// Question to the user

var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("User:" + " " + userChoice);

// Computer's choice

var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

// Compare User and Computer's choice

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";
            }
        }
    };
   
//call function 

compare(userChoice,computerChoice);

Testing: check if correct answer appears ...answers -= yes they are correct outputs.

output1:

User: rock
Computer: scissors
"rock wins" // rock smashes scissors
output2:

User: paper
Computer: scissors
"scissors wins" // scissors cuts paper

output3:
User: scissors
Computer: paper
"scissors wins"

i'd like to take credit for it but I can't in all fairness because it was a fellow learner on here who helped me clean up my code a bit too the other day as I have started last week so I am not far ahead of you as I tend to be slowly going through this course to build a good foundation definitely from the ground up haha.
hope it helps you iron out those minor errors.


#4

As reading code in plain text is really hard there are some formatting options that are explained here:

@finbarbartonmaunsell One problem is definitely the lack of a } here:

*else if ( choice1 === "rock") {*  
     *if (choice2 === "scissors") {*
        *return "rock wins";*
        *}*
    *else {*
        return "paper wins";} 
        *}

a mistake that you did not make for the following cases :smile:
PS: The asterisks are meant to make the text italic e.g. italic text *italic text*
Also you can make the text bold by **bold text**.