9/14 - Always coming up tie


#1

I’ve seen this question posted a couple times, but so far the answers don’t seem applicable to what I’ve written. But maybe that’s just a beginner’s mistake. Here’s what I have so far, the code should be pretty close to what’s included in the hints.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors')
  {
    return userInput;
  }
  else
  {
    return 'Oops!';
  }
};

// Computer makes a choice
const getComputerChoice = () => {
  let randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0: return 'rock'; break;
    case 1: return 'paper'; break;
    case 2: return 'scissors'; break;
  }
};

const determineWinner = (userChoice, computerChoice) => {
	// results of a tie
  if (userChoice === computerChoice) {
    return 'It is a tie!';
  }
  
  // results if user selects 'rock'
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer wins!';
    }
    else {
      return 'You win!';
    }
  }
  
  //results if user selects 'paper'
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'The computer wins!';
    }
    else {
      return 'You win!';
    }
  }  

  //results if user selects 'paper'
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer wins!';
    }
    else {
      return 'You win!';
    }  
	}
};

// Checking results against choices
console.log(getComputerChoice());
console.log(getUserChoice('rock'));
console.log(determineWinner());

Here’s a sample of the output; the “determineWinner()” text is always “It is a tie!”

scissors
rock
It is a tie!

Thanks for your help!


#2

Without looking at the code I would guess that there is a condition somewhere deciding whether it’s a tie.
Right before that would be a good place to investigate, for example, which two values are being compared there?


#3

The code used for comparison is this…

const determineWinner = (userChoice, computerChoice) => {
	// results of a tie
  if (userChoice === computerChoice) {
    return 'It is a tie!';
  }

And the console.log for getComputerChoice() and `getUserChoice(‘rock’) is intended to show that the respective values are unequal.


#4

I don’t need to look at it!
You need to look at what values those variables refer to.
You don’t need me.

You can tell all by yourself that the condition there evaluates to true.
And you can tell all by yourself that this happens when those two variables refer to values that are equal.
What is the conclusion of that? Print them out to confirm. Actually, forget the logic and just print them out - look at what happens in your program.

When all your program does is one thing after the other, when there’s a bug, all you need to do to figure out what it is, is to drop in a few prints to show what is happening there.


#5

Thanks for the pointers @ionatan, I’m still figuring this stuff out. Through trial and error I think I’m learning how/where to insert console.log(...) into the code to figure out what values are being passed at given times. For example adding

console.log(userChoice + ' is userChoice');
console.log(computerChoice + ' is computerChoice');

into the determineWinner() function revealed that both variables are undefined; which explains why the if() that evaluates them resolves to true. Now I’m trying to figure out how to define their values based on the results of the getUserChoice() and getComputerChoice() functions.

Each of those functions has their own locally declared variables and I think this is my next challenge: e.g., if getUserChoice() returns the value of userInput, how do I assign that value to userChoice, which is defined in the determineWinner() function?

I don’t recall anything in the Codecademy JS lesson covering this . Still working this out…


#6

Got it! Seems the code with my functions was fine. The problem arose when I was calling the functions at the end of the code; I wasn’t passing any arguments into the determineWinner() function. This worked…

/*
console.log(getComputerChoice());
console.log(getUserChoice('rock'));
console.log(determineWinner());
*/
console.log(determineWinner(getUserChoice('rock'),getComputerChoice()));

#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.