Exercise 7. benn stuck on this exercise for 2 years and still cant figure it out. Need help


#1

i started learning to code about 2 years ago, i was doing very well until i got to this exercise 7, i spent months trying to figure it out, i failed so i decided to abandon the whole coding thing. 2 years later now im back and once again everything was going smooth until i find myself facing the same exerc 7 where i'm currently stuck. i've read other people suggestion, i even copy pasted other people answers and still i won't pass. i'm starting to wonder WHAT THE ■■■■ IS REALLY GOING ON WITH THIS FUCKING EXERCISE?

check out my code bellow.
please be very precise and try to avoid all the complex explanations that wont serve me much. i've read all types of explanation from dozens of people that hasn't help much thanks in advance.

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

#2

@hottcode,

Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on

  • the build of the IF ELSE-IF ELSE-IF statement structure (and keep in mind a pair-of-curly-brackets-{ } encapsulates a so-called =code-block=, a set of statement's which should stand together )

#3

In the if within the else if, you need to open the code block with { before the return, there currently is a closing } but it hasn't been opened:

if(choice2 === "scissors")

    return "rock wins";
}

should be

if(choice2 === "scissors") {

        return "rock wins";
    }

Same before return "paper wins".

Hope that works, if not, I'm as stumped as you.


#4

Well the general idea is the following you compare user and computerChoice by using a function. Now you'd have to consider 9 cases RvR, RvP, RvS, PvR,PvP,PvS,... and to reduce this we first deal with the tie case. So far so good now the idea is that we first of all only look at one choice (choice1). So we have 3 possible cases left choice1 being rock,paper or scissors. So what we do now in order to finish the is to build inside of these choices for choice1 an if else that handles what is left for choice2 so for an example see the hint of 6/9. And the exercise 6-8 all build one of these cases an outer else if for choice1 and an if else condition for choice2.

The problem in your code is that this structure got messed up, it was already wrong in 6/9 but there was no syntay error yet. Let's have a look at it (your code formatted not changed):

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

as you can see the else paper wins which should be the default choice for choice2 being scissors is now the default choice for your else if. Which leads to the problem that your condition starting with the tie case is now finished, there can't be an else if after an else. Even worse there is another } following which closes your function so when the next else if starts (the one you wrote for ex 7) it is outside of the function and not connected to an if so it produces an error.

As said for a correct example have a look at the hint of 6/9


#5
 if(choice2 === "scissors") 
            
            return "rock wins";
        }

You are missing parenthesis. Take a look at the code :

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

Same mistake for next if blocks too.


#6

Ohhh man it works. the problem was with the braces {}. but i also realized that the issue started on exercise 6, it was wrong but for some reason it was validated. thank you so very much for the help.


#7

you're absolutely right mrhellism. it was always the braces, it finally worked. thank you very much.


#8

thank you for the help haxor789.


#9

Good job finally figuring it out.
(I've been stuck for a long time too, and still can't figure it out. The thing i'm stuck on though is your grammar.)


#10

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") // clean,no {

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

else if (choice1 === "paper" ) // no {
if (choice2 === "rock") {
return "paper wins"
}
else {
return "scissors wins"
}
}
// DO NOT PUT { AFTER ELSE IF CODE,IF YOU DO THAT,IT WONT WORK!


#11

Hey thanks a lot cause I couldn't figure it out. It was those darn brackets.