What if choice1 is scissors?


#1

Hi, I'm having trouble figuring out what is wrong with my code; it says there is a syntax error, but I just can't seem to find the error.

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

console.log(compare(userChoice, computerChoice))

Sorry if it looks confusing, please feel free to give me any tips in general if there is something that can be improved


#2

sorry I can't check there's a :wink: emoticon on your post. but if I want to check:

  1. I open an new blank window
  2. F12 for developer tools
  3. Go to console tab, and copy past your answer there, you'll see the error

#3

Hi @dchappa make it easier for others to read by highlighting your pasted code and selecting the '</>' symbol from the tools in the message text box (directly above any replies/messages you write). If it doesn't make any difference, ensure there are spaces between you code and any introductory message.

This will certainly encourage more input I believe. I only add this comment since you addressed the issue yourself ("sorry if it looks confusing, please feel free...."). Unfortunately I have also drawn a blank in passing this segment. Hence why I was reading your post. Hope it gets solved soon. Best of luck.


#4

I would agree with @javarockstar69728 that the line with the emoji. :wink: is done by ;) so I guess you need to get rid of the semicolon there. Also else has no condition after it either but from the structure I'd guess that it should have been an else if anyway, shouldn't it?

And for formatting I'd add to what @c_daniels already said that you can do inline formatting by using backticks and syntax highlighting by using 3 backticks e.g.

```
//this is a test
for while function if else 
```

or by starting a line with 4 spaces.


#5

@haxor789 I could do with a hand with the following. I have literally no clue how to specify to the computer that choice1 is userChoice and choice2 is computerChoice in order to link the two parts of the code separated by my commenting in the middle. See below. Any help would be greatly appreciated:

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);

//these two lines are the offensing code. 
//var userChoice = choice1;
//var userChoice = choice2;
//Need to reference userChoice and computerChoice as choice1 and choice2 in order for the following code to work????

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

#6

This is done by the function call. By using

compare(userChoice,computerChoice)

you assign userChoice (the first argument) to choice1 (the first parameter) and computerChoice (the second argument) to choice2 (the second parameter).


#7

@haxor789 thanks so much. It was staring right at me.