Functions not working properly

Hey there!

This is the first project from the functions lesson, where we have to build a rock, paper, scissors game. I can’t understand why two of the determineWinner functions are not working properly. I have attached the whole exercise with comments giving details of what’s happening.

Hope you can help me cause I honestly can’t find where the mistake is.

Thanks in advance!

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

const getComputerChoice = computerInput => {
  computerInput = Math.floor(Math.random() * 3);
  switch (computerInput) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }    
}

const determineWinner = function(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'It\'s a tie!';
  } 
  if (userChoice === 'bomb') {
    return 'You Win!';
  }
  if (userChoice === 'rock' && computerChoice === 'paper') {
    return 'Computer Wins!';
  } else {
    return 'You Win!'; //All conditions work
  }
  if (userChoice === 'paper' && computerChoice === 'scissors') {
    return 'Computer Wins!';
  } else {
    return 'You Win!'; //Prints else condition where it should be if. Tie woks. Else condition works when true
  }
  if (userChoice === 'scissors' && computerChoice === 'rock') {
    return 'Computer Wins!';
  } else {
    return 'You Win!'; //Same as previous variable
  }
}

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

playGame();

One trusts you are still working to resolve the issues and haven’t taken that close a look, yet. Early on this caught my eye…

const computerChoice = getComputerChoice();

There is no argument in the call, so one expects there is no parameter at the receiving end, or at least a default parameter. It may not raise an error, though one expects it should, but there should only be () on the function.

const foo = () => {}

Will give a closer look to the rest of your code, presently.

1 Like

Thank you!

At some point I will go over it from scratch and see if I can get a different outcome. In the meantime I’ll move on with the course.