Having issues with conditions and functions

Not sure what i’m doing wrong. Not understanding why things keep coming back as undefined?

1 Like

Hey @webdev19,

Your code seems to work alright for me… are you still having trouble?

well whenever i test different answers it comes back as undefined

I think somehow i left out the function for the RNG computer so it has no values for the RNG computer.

I see one problem, have you made sure that you have curly braces on all of your if/else statements?

Look at this block of code again:

if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
     } else {
       return 'You won!';
     } 
 if (userChoice === 'paper') {
   if (computerChoice === 'scissors') {
     return 'The computer won!';
   } else {
     return 'You won!';
   } 
  if (userChoice === 'scissors') {
     if (computerChoice === 'rock') {
        return 'The computer won!';
     } else {
       return 'You won!';
     }
   }
 }

well i always try to open and close them immediately . Im in my first few days of javascript so i havent really figured out a way to keep up with all the braces lol

Yes, they can get rather confusing. That’s why you have to make sure you closed them the correct way:

if (userChoice === 'rock') { //where is the closing curly brace for this if/else statement?
    if (computerChoice === 'paper') {
      return 'The computer won!';
     } else {
       return 'You won!';
     } 
 if (userChoice === 'paper') { // and this one?
   if (computerChoice === 'scissors') {
     return 'The computer won!';
   } else {
     return 'You won!';
   } 
  if (userChoice === 'scissors') { // and looking at your original code, you may have too many over here
     if (computerChoice === 'rock') {
        return 'The computer won!';
     } else {
       return 'You won!';
     }
   }
 }

I guess i can try to start from the beginning but i dont really know what else to do other than just open and close them as soon as i start an expression.

Right, but have you closed them on those if-else loops?

Let’s take this one at a time:

if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
     } else {
       return 'You won!';
     } 

This is your full code for the first choice of “rock”. Let’s check the curly braces:

if (userChoice === 'rock') { 

first line of code. Whoooa looks like there is no curly brace here. But that’s ok. We can put the content inside the if/else statement, and then add a closing curly brace.

if (computerChoice === 'paper') {
      return 'The computer won!';
     } else {
       return 'You won!';
     }

Next block, looks alright, you have all your closing curly braces.
Let’s put these together:

if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
     } else {
       return 'You won!';
     } 

Hmmmmm looks like we don’t have the closing curly brace from the first if/else statement at the end of this code. Let’s add this to the code at the very end of this code block.

if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
     } else {
       return 'You won!';
     }
} 

There! This code is completely free of errors. Do you see what you need to do now?

I’m just struggling to keep up with what is what even with you breaking it down when i go back to my code it just all runs together. smh

Your code runs off to the right quite a lot. (Your indentation)

There are two choices, one for each player
So at the left-most indentation level of your function, you’d have three cases, each possibility for choice1

choice1a
choice1b
choice1c

They’re testing the same thing so they should look the same, should be indented the same.

Under each such choice you’d test choice2

choice1a
  choice2a
  choice2b
  choice2c
choice1b
  choice2a
  choice2b
  choice2c
choice1c
  choice2a
  choice2b
  choice2c

That doesn’t run off to the right.
Also some of them can be eliminated, for example if they’re identical, so 1a2a 1b2b 1c2c can all be the same test

choice1 === choice2
choice1a
  choice2b (2 wins)
  choice2c (1 wins)
choice1b
  choice2a (1 wins)
  choice2c (2 wins)
choice1c
  choice2a (2 wins)
  choice2b (1 wins)

It can be made a fair bit smaller yet, but that’s what you already have so far.

The next thing that can be done is recognize that they’re mirrored, only half are actually unique relations between two choices.

So then one could list all ways to win (3 different ways). If it’s one of those, player1 wins, if equal, draw, otherwise player 2 wins.
That’s a total of 5 branches and probably matches how you’d explain the rules to a person.

1 Like