Rock, Paper, Scissors - always same output in "determineWinner" function


#1

Hello!

This is my first post here, so sorry in advance if it’s not up to the forum standards. I am currently doing the “Rock, Paper, Scissors” Introduction to JavaScript PRO content and one of the tasks is to make the code determine who is the winner by comparing the userChoice and computerChoice parameters. Even though it looks as if my code is identical to the one in the hints I get the “Tie” outcome regardless of what was the userChoice and computerChoice (e.g. in the console I’ll see the following:
“User chose: paper
Computer chose: scissors
The game was a tie”).

Could someone help me out here because I’m going nuts now? :slight_smile:

Thanks in advance.

(Code should be below, but I’m not sure I did the formatting right.)

Summary
const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log('Please select rock, paper, or scissors.');
  }
};
function getComputerChoice() {
  const randomNumber = Math.floor(Math.random() * 3);
  if (randomNumber === 0) {
    return 'rock'; }
  if (randomNumber === 1) {
    return 'paper'; }
  if (randomNumber === 2) {
    return 'scissors';
  }    
};

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'The game was a tie';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Computer wins.';
    } else {
        return 'User wins.'; }
      }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'Computer wins.';
    } else {
      return 'User wins.'; }
    }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'Computer wins';
    } else {
      return 'User wins.'; }
    }
};

function playGame() {
  const userChoice = getUserChoice('paper');
  const computerChoice = getComputerChoice();
  console.log('User chose: ' + userChoice);
  console.log('Computer chose: ' + computerChoice);
  console.log(determineWinner());
};

playGame();

#2

Java and JavaScript are different languages

pasting it back in and running it, seeing if the result is the same, is a good measurement of that

that should narrow it down to a single line shouldn’t it? Try debugging that without any of the rest. Probably a good idea to show that it’s actually there that it goes wrong by printing out information about what’s being done there (show that the issue is there before you dig deeper)


#3

Java and JavaScript are different languages

Of course you’re right - obviously the more one concentrates not to make a mistake, the more likely it will happen.

I found the cause since, I left out the parameters from console.log(determineWinner()); in the playGame function.


#4

And a route to figuring that out is -> THIS decision (condition for tie) goes wrong -> print out the data it depends on -> whoops, these are undefined where do they come from -> those are the parameters of the function, caller sent in wrong arguments -> find caller -> no arguments at all

(almost completely uninteresting what was wrong, how to figure it out on the other hand is something that can be applied to many similar situations)