Determine winner doesn't display when user picks paper


#1



Whenever I run the code and the user picks 'paper', this happens:
You threw: paper
The computer threw: scissors
There is no "The computer won!" whereas it should display that.
When the user picks rock or scissors, a winner gets determined.

Can someone tell me what I'm doing wrong?
(If you need the full code I can post that as well)


function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'It is a tie!';
  } 
  
  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!';
    }
    
    }
    }
  }
}

#2

a function only executes when called, add a function call so determineWinner will run


#3

I thought I already did that within the playGame function.
The problem is that it only doesn't display when the user picks paper, when the user picks something else it just displays.

function playGame () {
  var userChoice = getUserChoice();
  var computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  
  console.log(determineWinner(userChoice, computerChoice));
}

#4

this is not going to work this way, can i see your full code? Then i can help you a lot more efficient.


#5

sure :slight_smile:

function getUserChoice (){
  var userInput = prompt('Rock, Paper or Scissors?');
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

function getComputerChoice() {
  Math.floor(Math.random() * 3);
  if (getComputerChoice < 0.34) {
    return 'paper';
  }
  else if (getComputerChoice > 0.66) {
    return 'rock';
  }
  else {
    return 'scissors';
  }
}

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'It is a tie!';
  } 
  
  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!';
    }
    
    }
    }
  }
}

function playGame () {
  var userChoice = getUserChoice();
  var computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  
  console.log(determineWinner(userChoice, computerChoice));
}

playGame();

#6

if we temporary remove computerChoice from your determineWinner function, we get the following:

if (userChoice === 'rock') {
     if (userChoice === 'paper') {
          if (userChoice === 'scissors'){}
    }
}

so only if userChoice is rock, it will check userChoice is paper. it should obviously be:

if (userChoice === 'rock') {}
if (userChoice === 'paper') {}
if (userChoice === 'scissors'){}

#7

aaaah thank you very much! it works now :smiley:


#8