"What if choice1 is rock?" - Stuck on JavaScript functions


#1

Hello! I've been having troubles with seeing how this code doesn't exactly work. In my eyes, I see that I've followed the rules but have done something wrong, and I can't find where. Help would be much appreciated!

My JavaScript code:

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


Any help is appreciated!


#2

Start by establishing in what way it behaves differently from how it's supposed to behave. That'll say a lot about what needs to change.


#3

I don't see whats wrong with it, really. Could you point out any tips to help me in the future? I'm rusty with functions, I don't fully understand. Sorry for the ignorance. Thanks! :slight_smile:


#4

Sure but how is it behaving incorrectly? How are you able to tell that it isn't right? What is the issue?

Won't get anywhere without that. That is where you start.


#5

I just did it! I looked through really carefully and saw the instructions to write under an existing function. I removed the curly brackets,and there we go.

LESSON COMPLETE!


Thank you :slight_smile:

#6

End Result, for any stuck on this issue.

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

#7

I am now really confused on curly bracket placement as I have done the same thing on the next lesson and it was wrong. :confused:

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"


#8

They have meaning, it's not right or wrong, it's how you tell the interpreter how to read your code.

You use curly braces to group statements into a single statement which is executed together. For example, a function's statements are all grouped together by curly braces.

So, if you're wondering how they should be placed, you'll have to decide how they're supposed to be grouped and place the braces accordingly.

If you indent based on the braces in the code you posted above, you'll get:

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"

See how there are a couple of them missing in the end, how the code is far to the right but there's nothing that closes those scopes


#9

Well, having heard that made me think about placement differently and correctly.

Thank you.

I finished with this:

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

#10

Fix that indentation to match the braces though.

Yeah, it's kind of redundant to have both indentation and braces but.. keep them consistent.