#8 Hey I'm having trouble with my code. It keeps on returning "SyntaxError; Unexpected token ="


#1

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);
function = (choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
else if (choice1 === choice2) {
if (choicce2 === "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 === "paper") {
return "scissors wins";
}
else {
return "rock wins";
}
}
}
console.log(compare(userChoice, computerChoice));


#2

Hey @svennileslearn,
you got a few mistakes in your code.

  • Under console.log("Computer: " + computerChoice); you should declare a new function and assign it to the variable compare.

  • Inside the else-if of your main if-else-construct, you got a little typo-mistake in the inner if-construct. (choicce2 instead of choice2)

Hope this will help you :slight_smile:

P. S. Check out THIS post for your further threads :stuck_out_tongue:


#3

I've done that however JS only prints what the computer chose
This is the "improved 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 === choice2) {
if (choicce2 === "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 === "paper") {
return "scissors wins";
}
else {
return "rock wins";
}
}
compare(userChoice, computerChoice);
}


#4

Hey again @svennileslearn,


coughs


Well, never the less:
I corrected your code.

The mistakes you made:

  • In the first else ifyou wrote again (choice1 === choice2).
  • The last condition is an else, not an else if again.
  • Removed the unnecessary closing curly bracket }after your compare-call and putted it on the right place (before the compare-call) :stuck_out_tongue:

Here your corrected, and better formatted 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 (choice2 === "paper") 
        {
            return "scissors wins";
        }
        else 
        {
            return "rock wins";
        }
    } 
}

compare(userChoice, computerChoice);

Check the code and try to understand where your mistake have been made.
Hope I could help you :slight_smile:
If you got further questions, feel free to get in touch with me :smiley: