2.8 code doesnt execute right



i modified my code, so i can put in userChoice and computerChoice manualy, wanted to test all options.
But i had to realize that my code “stops” or something after “you lose1”. I’ve double checked if i ended the compare function, but it looks right (at least for me). Whats wrong?

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = prompt("?");
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
  console.log("You choose: " + userChoice);
 var compare = function(choice1, choice2)    {
    if (choice1===choice2)                      {
        return "The result is a tie!";          } 
    else if (choice1="rock")                    {
        if (choice2==="scissors")                   {
            return "you win1";                      }
        else                                       {
            return "you lose1";                    }
    else if  (choice1==="paper")                {
        if (choice2==="rock")                       {
            return "you win2";                      }
        else                                        {
            return "you lose2" ;                    }
     else if (choice1==="scissors")             {
        if (choice2==="rock")                       {
            return "you lose3";                     }
        else                                        {
            return "you win3";                  }
compare(userChoice, computerChoice)


Why are all the brackets so weird positioned? Anyway, there is a syntax problem in line 17:

else if (choice1="rock") 

take a close look, do you see it? you use a single equal sign, which means assign. You overwrite choice1 with "rock", you might want to compare instead


Haha, the weird positioning is only for me, it made it easier for me to see in witch part i am, and if i close everything correct. How do you write it to not forget when to close brackets?

Thx for the hint


Did this resolve your issue?

I don’t know, i just indent the code properly, then i see the curly brackets as well. It becomes easier at some point


Yeah issue resolved.
indent (strange translation) means this? :

else if (choice1===“scissors”)
if (choice2===“rock”)
return “you lose3” ;
return “you win3”;
Oh so much to learn, not even get why the brackets not at the beginning of the lines xD

// Oops, try again.
Your code returned ‘you lose3’ instead of ‘undefined’ when the inputs are scissors and rock//

whats this error about? I dont get why it should be undefined with scissors and rock


Good to hear your issue was resolved :slight_smile:

You can indent like so:

if (condition){
    /* do something */
if (condition){
   if (condition){
       /* do something */
   else {
       /* do something */

this way, you can quickly see where all the curly brackets are, and where they belong. some people prefer:

if (condition)
   /* do something */

just figure out which you like best


while still in the exercises, you should return “rock” or “scissors”, not youwin


so happy to hear it isnt my failure :smiley:


4 posts were split to a new topic: 2.8 code doesn’t execute right