I tried to expand to "rock, paper, scissors, lizard, spock" and failed


#1

I have tried to expand my rock, paper, scissors to "rock, paper, scissors, lizard, spock" I now keep getting a message saying unexpected end of input. Here is my code:

var userChoice = prompt("Do you choose rock, paper, scissors, lizard or spock?");
var computerChoice = Math.random();
    if (computerChoice <= 0.20) {
	computerChoice = "rock"; 
    }
    else if (computerChoice <= 0.40) {
	computerChoice = "paper";
   
     if (computerChoice <= 0.60) { 
    computerChoice ="scissors";
     
        if (computerChoice <= 0.80) {
        computerChoice = "lizard";
        }
           else {
        computerChoice = "spock"; {   
                console.log("Computer: " + computerChoice);
        }
           };

 var compare = function(choice1,choice2) {
      
     if (userChoice === computerChoice) {
        console.log("The result is a tie!");
     }
        else if (userChoice === "rock") {           
           if (computerChoice === "scissors") {
              console.log("rock wins");
           if (computerChoice === "paper") {
              console.log("paper wins");
            if (computerChoice === "lizard") {
              console.log("rock wins");
           } 
           else {
              console.log("spock wins");
          }
     } 

     else if (userChoice === "paper") {
           if (computerChoice === "rock") {
               console.log("paper wins");
            if (computerChoice === "scissors") {
               console.log("scissors wins");
            if (computerChoice === "lizard") {
               console.log("lizard wins");  
           }
           else {
              console.log("paper wins");
          }
     } 

     else if (userChoice === "scissors") {
           if (computerChoice === "rock") {
              console.log("rock wins");
            if (computerChoice === "paper") {
              console.log("scissors wins");
            if (computerChoice === "lizard") {
              console.log("scissors wins");  
           } 
           else {
              console.log("spock wins");
          }
     } 
     
     else if (userChoice === "lizard") {
           if (computerChoice === "rock") {
              console.log("rock wins");
            if (computerChoice === "paper") {
              console.log("lizard wins");
            if (computerChoice === "scissors") {
              console.log("scissors wins");
           } 
           else {
              console.log("lizard wins");
          }
     } 
     else if (userChoice === "spock") {
           if (computerChoice === "rock") {
              console.log("spock wins");
            if (computerChoice === "paper") {
              console.log("paper wins");
            if (computerChoice === "scissors") {
              console.log("spock wins");
           } 
           else {
              console.log("lizard wins");
          }
     };

The curly brackets appear to be my Achilles's heel. Can somebody point me to my error or a posting that can help me to learn more about this? JavaScript is by far the hardest to learn and I am failing miserably.


#2

Ok yes there seem to be a couple of problems with the curly brackets. A general hint would be to mark levels of nesting by indentation e.g. whenever you open a { you indent your code by a tab or at least a few spaces so that it is visible that something started here e.g.

    if (computerChoice <= 0.20) {
    	computerChoice = "rock"; 
    }
    else if (computerChoice <= 0.40) {
    	computerChoice = "paper";
        if (computerChoice <= 0.60) { 
            computerChoice ="scissors";
            if (computerChoice <= 0.80) {
                computerChoice = "lizard";
            }
            else {
                computerChoice = "spock"; {   
                    console.log("Computer: " + computerChoice);
                }
            };
This way it should be easier to keep track of where you're blocks of code {} begin and end. Also here:

computerChoice = "spock"; {

you don't need a start of a block. You can also use jsbeautifier.org to make the formatting for you but the easiest way is to do it properly while writing the code.