Newb need help


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

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

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'The game was a tie.'; {
	if (userChoice === 'rock') {	
  	if (computerChoice === 'scissors') {
    return 'You have won!';
  	} else {
    return 'The computer won.';
			}
		}
 
if (userChoice === 'paper') {
  if (computerChoice === 'rock') {
    return 'You have won!';
  } else {
    return 'The computer won.';
  }
}

if (userChoice === 'scissors') {
  if (computerChoice ===  'paper') {
    return 'You have won!';
  } else {
    return 'The computer won.';
  			}
			}	
	  }
	}
}

const playGame = () => {
  const userChoice = getUserChoice('rock', 'paper', 'scissors');
  const computerChoice = getComputerChoice();
  console.log('Your choice was: ' + userChoice);
  console.log('The computer is: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}


playGame();

Now I am getting no error messages but my winner part is undefined :thinking:


#2

this is your determineWinner function:

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'The game was a tie.'
  }
}

why are the rest of the comparisons not nested inside the determineWinner function? userChoice is the parameter of determineWinner function, so this variable only exist within the function (local scope)


#3

thanks that helped!
but im still having issues, im thinking about just making a big “else if” instead


#4

Show me what you got and where you think you go wrong, i will see if i can help you further


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

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

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'The game was a tie.'; {
	if (userChoice === 'rock') {	
  	if (computerChoice === 'scissors') {
    return 'You have won!';
  } else {
    return 'The computer won.';
	}
}
 
if (userChoice === 'paper') {
  if (computerChoice === 'rock') {
    return 'You have won!';
  } else {
    return 'The computer won.';
  }
}

if (userChoice === 'scissors') {
  if (computerChoice ===  'paper') {
    return 'You have won!';
  } else {
    return 'The computer won.';
  			}
			}	
	  }
	}
}

const playGame = () => {
  const userChoice = getUserChoice('rock', 'paper', 'scissors');
  const computerChoice = getComputerChoice();
  console.log('Your choice was: ' + userChoice);
  console.log('The computer is: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}

playGame();

#6

we need the following structure:

  if (userChoice === computerChoice) {
    return 'The game was a tie.'; 
  } else if (userChoice === 'rock') {
     // if computerChoice is scissors, user wins else computer wins
  } else if (userChoice === 'paper'){

  }

so we need 1 if to check for tie, then 3 else if to check the different userchoice option. Within each else if statement nest an if and else for computerchoice to determine winner


#7

Nice its working great now
thanks!


#8