Unknown 'You win!' gets logged to console


#1

I already boiled down to where the issue resides, i just can’t seem to find the issue.
Beneath is the part of the code that has wrong formatting or if/else statements where a ‘You win!’ gets logged to the console.
I can’t seem to figure out where it comes from.

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    console.log('It was a Tie!');
  };
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer Wins!';
    } else {
    	return 'You win!';
    };
  };
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors'){
      return 'The computer Wins!';
    } else {
      return 'You win!';
    };
  };
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock'){
      return 'The computer Wins!';
    } else {
      return 'You win!';
    };
  };
};

Any help on this will be really apreciated, I’m probably missing something trival…

Here is the complete code:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput
  } else {
    console.log(`Error! ${userInput}, is not a valid choice!`);
  }
};

const getComputerChoice = () => {
  randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
      case 0:
      	return 'scissors';
      case 1:
      	return 'scissors';
      case 2:
      	return 'scissors';
    default:
      console.log('Error making computer choice')
                      }
      
};

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    console.log('It was a Tie!');
  };
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer Wins!';
    } else {
    	return 'You win!';
    };
  };
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors'){
      return 'The computer Wins!';
    } else {
      return 'You win!';
    };
  };
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock'){
      return 'The computer Wins!';
    } else {
      return 'You win!';
    };
  };
};

const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice('scissors');
  console.log(`You picked: ${userChoice}`);
  console.log(`The computer picked: ${computerChoice}`);
  console.log(determineWinner(userChoice, computerChoice));
}

playGame()

#2

lets say userChoice and computerChoice are both rock, then here:

if (userChoice === computerChoice)

this condition is true, so the function logs that the result is a tie. Then it moves on to this condition:

if (userChoice === 'rock')

which is also true, so then it checks if computerChoice is paper, its not, so you win

you could prevent this by using else if, else if only runs when previous if and else if statements evaluate to false, but in order for this to work, you would have to get rid of some unnecessary semi-colons.

you could also return that the result is a tie, a return keyword will end the function

personally, i would make both improvements i suggested


#3

Thanks, I made the if statement into a else if like you said, and instead of logging it to the console it now returns it.
works like a beautiful rock paper scissor’s game now.


#4

I hope you made the other if statement into else if statement as well (which check if userChoice is paper or scissors) and that you got rid of all the unnecessary semi-colons, then it should work well.


#5

I currently reformatted it to this:

const determineWinner = (userChoice, computerChoice) => {
	if (userChoice === computerChoice) {
    return 'It was a Tie!';
  } else if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer Wins!'
    } else {
    	return 'You win!';
    }
  } else if (userChoice === 'paper') {
    if (computerChoice === 'scissors'){
      return 'The computer Wins!';
    } else {
      return 'You win!';
    }
  } else if (userChoice === 'scissors') {
    if (computerChoice === 'rock'){
      return 'The computer Wins!';
    } else {
      return 'You win!';
    }
  }
};

#6

Nicely done :slight_smile:

Any further questions?


#7

At the moment not, maybe in the Sleep Debt Calculator eventually… but wrong subform.


#8

That would require making a new topic, my further questions was about this RPS project


#9

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