Rock Scissors Paper - JS - Please help


#1

Hi there,

I'm very new to JS, and am not sure why my rock scissors paper script is not printing to the console.
f anyone can help, that would be amazing. thanks in advance.

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(randomNumber) * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1: 
      return 'paper';
    case 2:
      return 'scissors';
                      }
}

function determineWinner(userChoice, computerChoice){
  if (userChoice === computerChoice) {
    return 'The game 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));
  }

#2

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(){
  var random = Math.floor(Math.random() * 3);
  switch (randomNumber) { // you don't have variable randomNumber, so you need to declare it
    case 0:
      return 'rock';
    case 1: 
      return 'paper';
    case 2:
      return 'scissors';
  }
}

function determineWinner(userChoice, computerChoice){
  if (userChoice === computerChoice) {
    return 'The game 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(); // syntax error: getUserchoice() doesn't exist
  var computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('the computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
}

playGame(); // i didn't see where you call playGame() function

I want to say that I liked your solution :thumbsup:


#3

Thank you for your help.

Very much appreciated!!!


#4

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