2

Does anyone know why this may be causing an infinite loop. I just can't see why!

I feel the issue may be with my while loop under play to five.

The while loop should be stopping at 5 and when the player or computer reaches this the game should stop.

This is Rock, Paper, Scissors. The result of each game is either player win, computer win or draw.

The game should end once either player reaches 5 wins.

The problem is the game is not ending as intended!

function getInput() {
  console.log("Please choose either 'rock', 'paper', or 'scissors'.");
  return prompt("Please choose either 'rock', 'paper', or 'scissors'");
}

function getPlayerMove() {
  return getInput();
}

function randomPlay() {
  var randomNumber = Math.random();
  if (randomNumber < 0.33) {
      return "rock";
  } 
  else if (randomNumber < 0.66) {
    return "paper";
  } 
  else {
    return "scissors";
  }
}


function getComputerMove() {
  return randomPlay();
}

function getWinner(playerMove,computerMove) {
  var winner;
  if (playerMove === 'rock' && computerMove === 'scissors') {  
    winner = 'Player';
  }  
  else if (playerMove === 'scissors' && computerMove === 'paper') {
    winner='Player';
  }  
  else if (playerMove === 'paper' && computerMove === 'rock') {
    winner='Player';
  } 
  else if (playerMove === 'paper' && computerMove === 'scissors') {
    winner='Computer';
  }  
  else if (playerMove === 'rock' && computerMove === 'paper') {
    winner='Computer';
  }  
  else if (playerMove === 'scissors' && computerMove === 'rock') {
    winner = 'Computer';
  }  
  else { 
  winner = "tie";
  }
  return winner;
}

// A big limitation of this game is the user is only allowed to choose once! To allow more choices you'd need to design the program differently 

function playToFive() {
  var playerWins = 0;
  var computerWins = 0;
  var playerMove = getPlayerMove();
  while ((playerWins <= 5) || (computerWins <= 5)) {
    var computerMove = getComputerMove();
    var winner = getWinner(getPlayerMove, getComputerMove);
    console.log('The player has chosen ' + playerMove + '. The computer has chosen ' + computerMove);
    if (winner === "player") {
        playerWins += 1; 
    } 
    else if (winner === "computer") {
        computerWins += 1;
    } 
    if ((playerWins = 5) || (computerWins = 5)) {
        console.log("The game is over! " + "The " + winner + " has taken out the game!");
        console.log("The final score was. " + playerWins + " to " + computerWins);
    }
    else {
        console.log("The " + winner + ' takes the round. It is now ' + playerWins + ' to ' + computerWins);
    }
  }
}

playToFive ();
2
  • If the loop should end when either player reaches 5, the comparisons should be < and the tests should be combined with && and not ||. With ||, the loop will continue so long as the loser has less or equal to 5. Commented Feb 16, 2016 at 6:41
  • if ((playerWins = 5) || (computerWins = 5)) should be if ((playerWins == 5) || (computerWins == 5)) Commented Feb 16, 2016 at 6:48

5 Answers 5

1

Problem is this line

var winner = getWinner(getPlayerMove, getComputerMove);

you are passing the function reference to the getWinner() method

var winner = getWinner(playerMove , computerMove );

Which means that you need to get moves again later, so change your method to (multiple issues fixed in your code)

function playToFive() 
{
  var playerWins = 0;
  var computerWins = 0;
  while ((playerWins <= 5) && (computerWins <= 5))  //this line has && instead of ||
  {
    var computerMove = getComputerMove(); 
    var playerMove = getPlayerMove(); //this line is now inside while loop
    var winner = getWinner( playerMove , computerMove );
    console.log('The player has chosen ' + playerMove + '. The computer has chosen ' + computerMove);
    if (winner === "Player") { //P caps
        playerWins += 1; 
    } 
    else if (winner === "Computer") { //C caps
        computerWins += 1;
    } 
    if ((playerWins == 5) || (computerWins == 5)) { //= is replaced by ==
        console.log("The game is over! " + "The " + winner + " has taken out the game!");
        console.log("The final score was. " + playerWins + " to " + computerWins);
    }
    else {
        console.log("The " + winner + ' takes the round. It is now ' + playerWins + ' to ' + computerWins);
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

In order for this if to run accordingly:

if ((playerWins = 5) || (computerWins = 5)) {}

You'll need to use the == operator, because just one = is used for value assignation.

if ((playerWins == 5) || (computerWins == 5)) {}

Comments

0

Firstly in the while loop you need to change it too a && statement otherwise if the computer is 8 and the player is 3 the loop will continue until both players are >5.

Comments

0

I Think first you should understand the difference between (==) and (===) here.

Use This Code (Conditional Code not assignment)

 if ((playerWins == 5) || (computerWins == 5)) { //use logical Operator not assignment Oprtr.
        console.log("The game is over! " + "The " + winner + " has taken out the game!");
        console.log("The final score was. " + playerWins + " to " + computerWins);
    }

Instead of ##

 if ((playerWins = 5) || (computerWins = 5)) { // Error Code.
            console.log("The game is over! " + "The " + winner + " has taken out the game!");
            console.log("The final score was. " + playerWins + " to " + computerWins);
        }

Problem is this line

you are passing the function reference to the getWinner() method

use this

 var winner = getWinner(playerMove , computerMove );

instead of

 var winner = getWinner(getPlayerMove, getComputerMove);

For This Function...

function getWinner(playerMove,computerMove) {
  var winner;
  if (playerMove === 'rock' && computerMove === 'scissors') {  
    winner = 'Player';
  }  
  else if (playerMove === 'scissors' && computerMove === 'paper') {
    winner='Player';
  }  
  else if (playerMove === 'paper' && computerMove === 'rock') {
    winner='Player';
  } 
  else if (playerMove === 'paper' && computerMove === 'scissors') {
    winner='Computer';
  }  
  else if (playerMove === 'rock' && computerMove === 'paper') {
    winner='Computer';
  }  
  else if (playerMove === 'scissors' && computerMove === 'rock') {
    winner = 'Computer';
  }  
  else { 
  winner = "tie";
  }
  return winner;
}

Every time this method set tie as winner.

But you should know the difference between (==) equalto and (===) equal value and equal type here

Comments

-1

Thanks guys. There were quite a few issues.

Cheers guys. Definitely needed && rather than ||. The || was resulting in the game continuing even

Also var winner = getWinner(getPlayerMove, getComputerMove) was required. This was causing an infinite loop.

1 Comment

Don't post another answer that just recaps what others told you, upvote and accept accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.