Rock, paper, or scissors...frustration


#1

There seems to be a syntax error, but I am unsure of where it is.

...

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice)

if (computerChoice <= .33) {
computerChoice = "rock"
} else if(computerChoice >= 0.34) {
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";
}

}

...


#2

this bit:

console.log("Computer:" + " " + computerChoice);
{

you have a { after the onsole.log for unknown reason


#3

That didnt fix the syntax error, I have the following If-else if-else sequence correct from my understanding but there notifies of an error.


#4

Hi csssurfer,

With regards to syntax, have a good look at where your curly brackets open and close in your if/else statement. It seems you have an extra opening curly bracket after the

else if (choice 1 === "rock")

line which will be causing a syntax error.

Although not strictly necessary in this case, I personally find it useful to write functions that contain the body of the function in curly brackets as in the following way.

var something = function(parameter1, parameter2) {
    _whatever the function does_
}

That way I know it is contained and won't contaminate the rest of the code if I change it. Incidentally, you do not need the curly brackets around the function definition like you currently have.

And finally, another problem you will be encountering is that you have written the function to compare, but not invoked or called it. Try adding:

compare(userChoice, computerChoice);

After the if/else statement.

Hope this helps, if not, let me know!

Mirrafoil


#5

There's one other thing I've just noticed about what the compare function returns, but I won't give everything away.

Think about what you want to do with the return value :wink:

Mirrafoil


#6

can you post an updated version of your code?


#7

the problem is fixed, but the change was a curly bracket. i changed this previously, but with no effect.

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
    console.log(computerChoice)

if (computerChoice <= .33) {
	computerChoice = "rock"
} else if(computerChoice >= 0.34) {
	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";
    }
}

...


#8

this line:

if (choice2 === "scissors")  // missing {

and your function opening curly bracket ({) should have a matching closing tag at the end, it doesn't