Can someone tell whats wrong ? I did everything like it says and still not working


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


#2

Seemingly you have left out an else if situation where user choice is scissors and the computer choice is paper.


#3

I tried and still got me an error " SyntaxError: Unexpected token { "


#4

This is working 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!";
}
//prvo ugnjezdenje
else if(choice1 === "rock") {
}
if(choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
//drugo ugnjezdenje
else if(choice1 === "paper") {
}
if(choice2 === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
};*/
var compare = function (choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie!";
}
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";
}
}
}
compare(userChoice, computerChoice);


#5

Thank you very much !


#7

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"
}
};
This isn't working for me.. can somone explain to me what I'm doing wrong?? I keep getting "SyntaxError: Unexpected token else"


#8

First, I think there is a missing curly brace,

if(choice1 === choice2) -----> // missing curly brace
return "The result is a tie!"
}

Second, we do not add semi-colons at the end of else if statment

else if (choice1 === "rock") {
if (choice2 === "scissors"){
return "rock wins"
}; else { -----> // Remove this semi-colon

Thirdly, we add semi-colons at the end of any statement between the curly braces

if(choice1 === choice2)
return "The result is a tie!" ----> // Missing semi-colon
}


#9

if(choice1 === choice2){
return "The result is a tie!"
}
else if (choice1 === "rock") {
if (choice2 === "scissors"){
return "rock wins"
} else {
return "paper wins";
}
}

I am still getting an error message.


#10

Try putting a semi-colon ( ; ) at the end of the return statements.


#11

This is my code to help you figure out what is the wrong with your code.

function compare(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';
}
}

};

Understand the code and figure out the missing but don't copy it :slight_smile:


#12

So I have to put
function compare(choice1,choice2){
if (choice1 === choice2)

otherwise I get the error. It didn't state that in the instructions. So do I have to put function compare in between all of the if/else if/ else statements?


#13

function compare(choice1,choice2) is exactly the same as var compare = function(choice1,choice2)
I have tested your code, and fixed it.

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

Before debugging

var compare = function(choice1, choice2) 
{
if(choice1 === choice2)
return "The result is a tie!"          //Missing semi-colon
}
else if (choice1 === "rock") {
if (choice2 === "scissors"){
return "rock wins"                // Missing semi-colon
}; else {                         // an excess semi-colon before 'else'
return "paper wins"                       // Missing semi-colon
}                                       //Missing curly brace
};

#14

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.