Javascript 8/9 revealed


#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";
}

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 "scissors wins";
        } else {
            return "rock wins";

        }
    }
}
console.log("The challenger has chosen" + " " + userChoice);
console.log("The computer has chosen" + " " + computerChoice);
compare(userChoice, computerChoice)

#2

As you can see after formatting, you have 2 elses in your paper case directly following each other.


#3

haxor789 you are so helpful! Thank you for looking out for all of us!!


#4

Hello, please could you have a look at my code...I have been stuck on it for days!!

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

console.log("The user has chosen" + " " + userChoice)
console.log("The computer has chosen" + " " + computerChoice)
compare(computerChoice,userChoice)

Error message:expected expression, got '}'
I did run it thru the "beautifier" & then wrote it out and matched my { }s in a way that seems clear...and still no luck. Thank you:)


#5

Sry but this looks neither beautified nor is there any order in your {}s.
I'd recommend that you take a step back and fix the cases one by one.

this is for example how you can indent your code:

var compare = function(choice1,choice2){
    if (choice1===choice2){
        return "The result is a tie!";
    }
    ...
}

start and end of a statement, like if and the function, are on the same (vertical) line the code in between is indented so now it should be obvious where something starts and ends.

If you check the hint of 6/9 you see how the structure of one of the other cases could look like:

else if(choice1 === "rock") {    
    if(choice2 === "scissors") {
        return "rock wins";
    }
    else {
        return "paper wins";
    }    
}

As you can see the idea is that you have a look at the userChoice (choice1) and determine in a nested if/else what the computerChoices (choice2) are (tie is already covered above).

As your cases currently look at least similarly wrong lets go through the errors in this case:

else if (choice1 === "rock")
       { if(choice2 === "scissors")}
    
   { return "rock wins";}
            
    else
               { return "paper wins";}

The first thing to notice is that the else if is closed far too ealry it should wrap all this as shown above but here it only wraps:

{ if(choice2 === "scissors")}

This statement now has the problem that it lacks something to do in case it is true. To be fair you wrote this here:

   { return "rock wins";}

but due to your {} they aren't considered as connected. Same for this one:

    else
               { return "paper wins";}

than should belong to the inner if but know stands lonesome which would create an error of its own.

I hope this is helpful maybe you can fix it because they look pretty similar but if not take a step back and do it one by one.


#6

Thank you very much for your help! I am going to go back and carefully re-do the exercises :sunglasses: