Need help with Rock Paper Scissors


#1

I think I have gotten close. Everything seemed to work until I put in the line to announce the winner. Now everytime I run it the computer choice and the user choice are identical.


/*User Choice*/

function getUserChoice() {
  var userInput = prompt('Choose wisely');
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  }
  else {
    console.log('Invalid choice');
  }
}
/*Computer Choice*/

function getComputerChoice() {
  var compChoice = Math.floor(Math.random() *2);
  if (compChoice === 0) {
    return 'rock';
  } 
  else if (compChoice === 1) {
    return 'paper';
  }
  else if (compChoice === 2) {
    return 'scissors';
  }
}
/*Who won?*/

function determineWinner(userChoice, compChoice) {
  if (userChoice === compChoice) {
    return 'Game was a tie';
    }
  else if(userChoice === 'rock') {
    if (compChoice === 'paper') {
      return 'The computer won!';}
    }
  else if(userChoice === 'paper') {
    if (compChoice === 'rock') {
      return 'You win!!';
    }
  }
  }
 /*Play game*/

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


#2

In case anyone sees this... I had an easy error with the math and I was missing a curly brace on each part of the function to determine the winner.

/*User Choice*/

function getUserChoice() {
var userInput = prompt('Choose wisely');
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
}
else {
console.log('Invalid choice');
}
}
/*Computer Choice*/

function getComputerChoice() {
var compChoice = Math.floor(Math.random() *3);
if (compChoice === 0) {
return 'rock';
}
else if (compChoice === 1) {
return 'paper';
}
else if (compChoice === 2) {
return 'scissors';
}
}
/*Who won?*/

function determineWinner(userChoice, compChoice) {
if (userChoice === compChoice) {
return 'A tie';
}
if (userChoice === 'scissors') {
if (compChoice === 'rock') {
return 'Computer wins!!';
} else {
return 'You won!';
}
}
if (userChoice === 'paper') {
if (compChoice === 'scissors') {
return 'Computer wins';
} else {
return 'You won!';
}
}
if (userChoice === 'rock') {
if (compChoice === 'paper') {
return 'Computer wins!!';
} else {
return 'You won!';
}
}
}

/*Play game*/

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


#3

Did you succeed with the lesson?


#4

Yes. Thank you. Might not be pretty but now I understand.


#5

Prettiness does not matter now, that is why you are learning :slight_smile:


#6

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