1

I have this code for a rock, paper, scissors game that is basically homework. I have double-checked and it seems to be fine, however, when I run it it says:

SyntaxError: Unexpected token else, 

any help will be very appreciated :) Please note that I am a newbie, so if the question is dumb, please be nice and help <3

I just edited the code a bit, since I had many "Overlook" mistakes. I also wanted to clarify that I need all the code located after the function statement to be inside the function, that is why I don't close the first { right away. PD: Now I get: SyntaxError: Unexpected token =

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

            else {
                return "Paper wins"; }    
            }
        }
}

compare(userChoice, computerChoice)
13
  • 6
    Indent your code correctly and look carefully at your braces. Commented Dec 31, 2015 at 20:28
  • 5
    You need to decide whether you're going to use curly braces with your conditions or not, because that's where the error is. I would suggest always using them. Commented Dec 31, 2015 at 20:29
  • 1
    here is your error else if{ at this block if (choice2 === "rock") { return "paper wins" else if{ return "paper wins" } Commented Dec 31, 2015 at 20:33
  • 1
    Ok, @Juhana and I bet your parents injected all this knowledge into you when you were born, so you grew up knowing it. Unfortunately, I just started learning it and am so overwhelmed trying to find my errors that I overlooked many of them, and felt so frustrated that brought my code here, hoping to find nice people who would help me instead of laughing at me, but thanks, your comments helped me a bunch Commented Dec 31, 2015 at 21:03
  • 1
    @L.Gonzalez it is SO's way of hazing its new comers, don't beat yourself up about it. SO is a cesspool of negativity, with only a few kind and helpful people. (I am not one of the nice ones) anyways, you will learn that asking questions here come at a price, don't take it personal. Commented Dec 31, 2015 at 21:10

4 Answers 4

1

Ok, to stay true to your homework, I kept the same format just fixed the issues.

here it is:

  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!";
    }
    if (choice1 === "paper") {
      if (choice2 === "rock") {
        return "Paper wins!";
      } else {
        return "Paper looses!";
      }
    } else if (choice1 === "rock") {
      if (choice2 === "scissors") {
        return "Rock wins!";
      } else {
        return "Rock looses!";
      }
    }
    if (choice1 === "scissors") {
      if (choice2 === "paper") {
        return "Scissors wins!";
      } else {
        return "Scissors looses!";
      }
    }


  }
  compare(userChoice, computerChoice)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot!! I just realized the way how curly brackets work thanks to your example.. (More like you did my homework for me :) anyhow, I realized that I can close every curly bracket after its respective if statement ends, and don't have to wait until the function ends :) thanks again :)
1

Consider re-writing it in a little easier way.

See fiddle https://jsfiddle.net/DIRTY_SMITH/c7ww2hmz/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";
  }
  alert("the computer picked " + computerChoice);
  if ((computerChoice === "rock" && userChoice === "papper") || (computerChoice === "papper" && userChoice === "scissors") || (computerChoice === "scissors" && userChoice === "rock")) {
    alert("you won");
  } else if (computerChoice == userChoice) {
    alert("It's a tie");
  } else {
    alert("you loose");
  }

7 Comments

Thanks a lot for your help. Unfortunately there are a few things in your code that I have not even seen, but I will give it a shot.
Here is it with your current code and my modifications jsfiddle.net/DIRTY_SMITH/c7ww2hmz/4
The thing that you haven't seen, is it the && and ||?
@L.Gonzalez here is a little quick tutorial on the and's and or's jsfiddle.net/DIRTY_SMITH/c7ww2hmz/6
Hahaha, yes, I have not seen the following: || I copied it from your comment, don't know how to get those lines and alert either, but I already knew that && kinda means "and" ?
|
1

You will find your debugging much easier if you

  1. properly indent,
  2. use braces for any but the simplest if statements

For example:

if (choice1 == choice2) return "tie";         /* simple 1-line if is ok */
if (choice1 == "rock") {
    if (choice2 == "scissors") {              /* more complex, always use braces */
        return "rock wins";                   /* always indent nicely */
    } else {
        return "paper wins";
    }
}
/* ... and so on ... */

2 Comments

What if choice1 does not equal choice2 and it equals papper?
yes, you would need two more analogous sections for choice1 == "paper" and choice1 == "scissors" ...
1

Always properly format your code. You are missing a bunch of } before the else statements. Always use semi-colons at the end of a line (no, you don't technically need to be it is extremely good practice).

Also, you need to watch your equals. You had one ==== instead of ===

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

compare(userChoice, computerChoice)

3 Comments

Check it here first, get it working then re-post ;) jsfiddle.net/DIRTY_SMITH/c7ww2hmz/3
@AdamBuchananSmith my goal isn't go fix all of the OPs problems with his code. It's to point out the obvious mistakes and the ones related to his question. I did fix the wrong ==== he had that was also in my answer.
I am willing to bet by the OP's question, that it is a she and not a he. the heart shape at the end gives it away lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.