StavAk
StavAk

Reputation: 9

Rock-Paper-Scissors: the score is not recorded

I have created the function for the computer's choices and for the player's possible choices. However, for hours I've been trying to solve the issue of scorekeeping as it does not seem to work. I have set the requirements and I set global functions playerScore and computerScore = 0 and yet every time I call the gamescore function, the message which appears is "the tie" since for some reason the values of the two scores are always considered to be 0.

Any idea why might this be happening? I have done an extensive search online but in vain. I am still a beginner so on purpose, I use console.log and not innerHTML or any html/css style/button. The computerPlay() function is console-logged and every time go-live is refreshed, it gives a random choice so it works. For the second function, I do not know loops yet so I called it 5 times via console log to see the results. I also put specific playerSelection arguments so as to make the strings "You won, You lost, Draw again" cause if I did not, in all 5 times, the return was undefined and not one of these three strings/returns. If you refresh go live you will see the choices are changing randomly but the final sentence that should be given depending on the score after 5 rounds is always the same.

function computerPlay() {
  let gameWords = ["Rock", "Paper", "Scissors"];
  let randomWord = gameWords[Math.floor(Math.random() * gameWords.length)];
  return randomWord;
};
console.log(computerPlay());


let playerScore = 0
let computerScore = 0
let draws = 0

function playRound(playerSelection, computerSelection) {
  playerSelection = playerSelection.toLowerCase();

  if (computerSelection === "Rock") {
    if (playerSelection === "rock") {
      return "Draw again."
      draws++
    } else if (playerSelection === "paper") {
      return "You won!"
      playerScore++;
    } else if (playerSelection === "scissors") {
      return "You lost!"
      computerScore++
    };
  };
  if (computerSelection === "Paper") {
    if (playerSelection === "paper") {
      return "Draw again."
      draws++
    } else if (playerSelection === "rock") {
      return "You lost..."
      computerScore++
    } else if (playerSelection === "scissors") {
      return "You won!"
      playerScore++
    };
  };
  if (computerSelection === "Scissors") {
    if (playerSelection === "scissors") {
      return "Draw again."
      draws++
    } else if (playerSelection === "paper") {
      return "You lost..."
      computerScore++
    } else if (playerSelection === "rock") {
      return "You won"
      playerScore++
    };
  };
  console.log(playerScore);
};
const computerSelection = computerPlay();

console.log(playRound("RoCk", computerSelection));
console.log(playRound("Scissors", computerSelection));
console.log(playRound("PAper", computerSelection));
console.log(playRound("paper", computerSelection));
console.log(playRound("PAPER", computerSelection));

function gameScore() {
  if (playerScore > computerScore) {
    return "You won the game!";
  } else if (playerScore < computerScore) {
    return "You lost, try again.";
  } else if (playerScore === computerScore) {
    return "It is a tie.";
  }
}
console.log(gameScore());

Upvotes: 1

Views: 116

Answers (1)

Kazys
Kazys

Reputation: 477

You code returns and then increase counters:

return "Draw again."
draws++

as you return counter increase is not called.

Upvotes: 2

Related Questions