Rock Paper Scissors game

Hey guys so I am working on this game and I feel I am getting on something really basic. I don’t know if I am missing something but this is the code at its very basic level:

 function playRound(playerSelection,computerSelection){
    if (playerSelection == 'rock' && computerSelection == 'rock' ) {
        return console.log('TIE');
    }
    else {
        return console.log('OK');
    }
    
    
       }
  
const playerSelection = 'rock';
const computerSelection = 'rock';

but this code keeps returning OK as opposed TIE. Why is the first half of the if statement not activating? I feel like I am missing something crucial here.

Hello.

How are you calling your playRound() function? I put your code in this replit and put playRound(playerSelection,computerSelection) and playRound() at the bottom. The first one, when I pass in the variables on line 12 and 13, returns TIE.

The second returns OK because the function isn’t getting any arguments. As a result, playerSelection and computerSelection are undefined. So playerSelection == 'rock will result in false since undefined is not abstractly equal to 'false'. Same issue when you check the computerSelection variable. So because they both return false, your code goes to the else.

Uncomment one and then the other to see the difference. A couple of tips:

Use === instead of ==. Do a google search like: == vs === javascript to see the difference. Also, notice on lines 2 and 3 how I’m using the console.logs. It’s a quick way to check the value of your variables. If you look at the console on the right, you’ll see that they are both undefined.

Let me know if you have questions about what I shared.

1 Like

Ugh. Yes, I was using playRound() to call the function. I knew it was something obvious.

As for the === and ==, I will definitely look into and use the appropriate one moving forward. The console.logs you used are SUPER useful, thank you. Thanks for the help!!:smiley: