Please help me I need an answer soon


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


My code answers that there is an unexpected token {
I don't know why it says this but I need an answer for a genius hour project due soon

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (userChoice === "rock") {
    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");
                };
            };
        };
        compare(userChoice,computerChoice);
}
else if(userChoice === "paper") {
        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(choice1 === "paper")   {
                if (choice2 === "rock")         {
                    return("paper wins");
                }
                else {
                    return("scissor wins");
                }
            };
        };
        compare(userChoice,computerChoice);
}
else if (userChoice === "scissors") {
    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 (choice1 === "scissors")    {
                if(choice2 === "rock"){
                    return("rock wins");
                }
                else if (choice2 === "paper") {
                    return("scissors win");
                }
            };
        };
        compare(userChoice,computerChoice);
    }
}
else { 
        confirm('This game only works if you put in rock, paper or scissors.');
};


#2

There are several critical errors in your code. Break it up and start to debug.

Curly braces are only followed by a semi-colon if they are part of an assignment:

var foo = function () {

};
var bar = {};

Otherwise, not.


#3

So semi-colons are only after things like variables ....

var compare = function(choice1, choice2)    {
            if(choice1 === choice2)   {
                return("The result is a tie!");   
            }
            else (choice1 === "rock")    {
                if(choice2 === "scissors")     {
                    return("rock wins");
                }
                else { 
                    return("paper wins");
                }
            }
        };     /* so put a ; here?*/

#4

Assignments and line statements. Where you have them now is correct. None are missing and none are misplaced.


#5

So if I put semi-colons ....

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (userChoice === "rock") {
    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");
                }
            }
        }; /* here... */
        compare(userChoice,computerChoice);
}
else if(userChoice === "paper") {
        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(choice1 === "paper")   {
                if (choice2 === "rock")         {
                    return("paper wins");
                }
                else {
                    return("scissor wins");
                }
            }
        }; /* here... */
        compare(userChoice,computerChoice);
}
else if (userChoice === "scissors") {
    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 (choice1 === "scissors")    {
                if(choice2 === "rock"){
                    return("rock wins");
                }
                else if (choice2 === "paper") {
                    return("scissors win");
                }
            }
        }; /* here... */
        compare(userChoice,computerChoice);
    }
}
else { 
        confirm('This game only works if you put in rock, paper or scissors.');
}; /*and here?*/

I'm not sure if there needs to be one at the bottom


#6

Your other two else if statements need to be inside the function, above this line...

Be sure to remove all the other lines that call the compare function from inside.

// code
var compare ...

};
compare(..., ...);

#7

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (userChoice === "rock") {
    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("scissor wins");
                }
            }
            else (choice1 === "scissors")    {
                if(choice2 === "rock"){
                    return("rock wins");
                }
                else if (choice2 === "paper") {
                    return("scissors win");
                }
            }
        };
        compare(userChoice,computerChoice);
}
else if(userChoice === "paper") {
        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("scissor wins");
                }
            }
            else (choice1 === "scissors")    {
                if(choice2 === "rock"){
                    return("rock wins");
                }
                else if (choice2 === "paper") {
                    return("scissors win");
                }
            }
        };
        compare(userChoice,computerChoice);
}
else if (userChoice === "scissors") {
    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("scissor wins");
                }
            }
            else (choice1 === "scissors")    {
                if(choice2 === "rock"){
                    return("rock wins");
                }
                else if (choice2 === "paper") {
                    return("scissors win");
                }
            }
        };
        compare(userChoice,computerChoice);

}
else{ 
        confirm('This game only works if you put in rock, paper or scissors.');
}

Is this what you mean?


#8

Sort of, but you have way too much code. Only about the first 40 lines belong, all the rest is excess. Have you been writing this by hand or copy and pasting. I'd suggest clear and start over, following the instructions.


#9

I did follow the code the first time I just tried to add something so people would know you could only use rock, paper or scissors


#10

When you have completed the JS track, then come back to this project and work on it some more. For now, just leave it as basic and simple as you can so it meets the current expectations. You will be much better equipped by then to really improve on the code.

For now, the code simply returns undefined if the use enters something that cannot be compared. That's enough error checking to meet our present needs.


#11

Thank you for the information I'm happy that your tried to help me, but some times stuff doesn't work out as planned. Thank you and keep on coding :smile:.


#12

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