Mihai Marincat
Mihai Marincat

Reputation: 1

rock paper scissors with two players in javascript

Hy there. I am new to javascript and i want to make a simple game of rock, papper scissors but after running the code i get the 2 prompted messages and the 'TypeError: playerOneChoice is not a function'. what did I do wrong ? thanks very much.

function getPlayerOneChoice(){
  var playerOneInput = prompt('Mihai what do you pick ?');
  playerOneInput = playerOneInput.toLowerCase();
  if(playerOneInput === 'rock' || playerOneInput === 'paper' || playerOneInput === 'scissors'){
    return playerOneInput;
  } else {
    console.log('Invalid Choice !!!');
  }
}

function getPlayerTwoChoice(){
  var playerTwoInput = prompt('Flavia what do you pick ?');
  playerTwoInput = playerTwoInput.toLowerCase();
  if(playerTwoInput === 'rock' || playerTwoInput === 'paper' || playerTwoInput === 'scissors'){
    return playerTwoInput;
  } else {
    console.log('Invalid pick !!');
  }
}

function determineWinner(playerOneChoice, playerTwoChoice) {
  if(playerOneChoice = playerTwoChoice) {
    console.log('Tie');
  }
    if(playerOneChoice === 'rock'){
    if(playerTwoChoice === 'scissors'){
      return 'Mihai you won !';
    } else {
      return 'Flavia you won';
    }
  }
 if(playerOneChoice === 'scissors'){
   if(playerTwoChoice === 'paper'){
     return 'Mihai you won!!!';
   } else{
     return 'Flavia you won!!!';
   }
 }
 if(playerOneChoice === 'paper'){
   if(playerTwoChoice === 'rock'){
     return 'Mihai you won';
   } else {
     return 'Flavia you won';
   }
 }  
}

function playGame() {
  var playerOneChoice = getPlayerOneChoice();
  var playerTwoChoice = getPlayerTwoChoice();
  console.log('Mihai\'s pick : ' + playerOneChoice());
  console.log('Flavia\'s pick : ' + playerTwoChoice());
  console.log(determineWinner());
}

playGame();

Upvotes: 0

Views: 333

Answers (2)

Weedoze
Weedoze

Reputation: 13943

There are 3 errors in your code

1. console.log('Mihai\'s pick : ' + playerOneChoice());

You are calling the function playerOneChoice which does not exist. You actually want to call the variable playerOneChoice. Remove the parenthesis.

2. console.log(determineWinner());

You are calling the method without parameter but you need both choice. Call it like determineWinner(playerOneChoice,playerTwoChoice)

3. if (playerOneChoice === playerTwoChoice)

You are assignin playerTwoChoice to playerOneChoice instead of comparing them. Use ===

function getPlayerOneChoice() {
  var playerOneInput = prompt('Mihai what do you pick ?');
  playerOneInput = playerOneInput.toLowerCase();
  if (playerOneInput === 'rock' || playerOneInput === 'paper' || playerOneInput === 'scissors') {
    return playerOneInput;
  } else {
    console.log('Invalid Choice !!!');
  }
}

function getPlayerTwoChoice() {
  var playerTwoInput = prompt('Flavia what do you pick ?');
  playerTwoInput = playerTwoInput.toLowerCase();
  if (playerTwoInput === 'rock' || playerTwoInput === 'paper' || playerTwoInput === 'scissors') {
    return playerTwoInput;
  } else {
    console.log('Invalid pick !!');
  }
}

function determineWinner(playerOneChoice, playerTwoChoice) {
  if (playerOneChoice === playerTwoChoice) {
    console.log('Tie');
  }
  if (playerOneChoice === 'rock') {
    if (playerTwoChoice === 'scissors') {
      return 'Mihai you won !';
    } else {
      return 'Flavia you won';
    }
  }
  if (playerOneChoice === 'scissors') {
    if (playerTwoChoice === 'paper') {
      return 'Mihai you won!!!';
    } else {
      return 'Flavia you won!!!';
    }
  }
  if (playerOneChoice === 'paper') {
    if (playerTwoChoice === 'rock') {
      return 'Mihai you won';
    } else {
      return 'Flavia you won';
    }
  }
}

function playGame() {
  var playerOneChoice = getPlayerOneChoice();
  var playerTwoChoice = getPlayerTwoChoice();
  console.log('Mihai\'s pick : ' + playerOneChoice);
  console.log('Flavia\'s pick : ' + playerTwoChoice);
  console.log(determineWinner(playerOneChoice,playerTwoChoice));
}

playGame();

Upvotes: 2

Alexander
Alexander

Reputation: 4527

Remove () from playerOneChoice() and playerTwoChoice()

It must be so:

console.log('Mihai\'s pick : ' + playerOneChoice);
console.log('Flavia\'s pick : ' + playerTwoChoice);

Upvotes: 0

Related Questions