{"size": 2997, "ext": "js", "max_stars_repo_path": "Rock Paper Scissors/scripts.js", "max_stars_repo_name": "seangoats/TOP", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Rock Paper Scissors/scripts.js", "max_issues_repo_name": "seangoats/TOP", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Rock Paper Scissors/scripts.js", "max_forks_repo_name": "seangoats/TOP", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 36.1084337349, "max_line_length": 84, "alphanum_fraction": 0.6913580247}
const results = document.querySelector("#results");
const rock = document.querySelector("#rock");
rock.addEventListener("click", () => playRound("rock", computerPlay()));

const paper = document.querySelector("#paper");
paper.addEventListener("click", () => playRound("paper", computerPlay()));

const scissors = document.querySelector("#scissors");
scissors.addEventListener("click", () => playRound("scissors", computerPlay()));
const playerScore = document.querySelector("#player-score");
const computerScore = document.querySelector("#computer-score");

//Variable to check if player won
let playerWins;
//Variable to store player and computer wins
let playerWinCount = 0;
let computerWinCount = 0;
//Function to select a choice for the computer!
function computerPlay() {
  let choices = ["Rock", "Paper", "Scissors"];
  return choices[Math.floor(Math.random() * 3)];
}
//Function to play a round
function playRound(playerSelection, computerSelection) {
  playerSelection = playerSelection.toLowerCase();
  computerSelection = computerSelection.toLowerCase();
  if (
    (playerSelection == "rock" && computerSelection == "paper") ||
    (playerSelection == "paper" && computerSelection == "scissors") ||
    (playerSelection == "scissors" && computerSelection == "rock")
  ) {
    countWins(-1);
    results.textContent = `You Lose! ${capitalize(
      computerSelection
    )} beats ${capitalize(playerSelection)}`;
  } else if (playerSelection == computerSelection) {
    results.textContent = `Draw! Both picked ${capitalize(playerSelection)}`;
  } else if (
    (playerSelection == "rock" && computerSelection == "scissors") ||
    (playerSelection == "paper" && computerSelection == "rock") ||
    (playerSelection == "scissors" && computerSelection == "paper")
  ) {
    countWins(1);
    results.textContent = `You Win! ${capitalize(
      playerSelection
    )} beats ${capitalize(computerSelection)}`;
  } else {
    results.textContent = `Error! ${capitalize(
      playerSelection
    )} is not a valid choice!`;
  }
  if (playerWinCount == 5 || computerWinCount == 5) {
    results.textContent = whoWon();
    playerScore.textContent = "Player: 0";
    computerScore.textContent = "Computer: 0";
    playerWinCount = 0;
    computerWinCount = 0;
  }
}

//Function to capitalise the choice
function capitalize(string) {
  return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase();
}
//Function to count player wins
function countWins(playerWins) {
  if (playerWins == 1) {
    playerWinCount++;
    playerScore.textContent = `Player: ${playerWinCount}`;
  } else if (playerWins == -1) {
    computerWinCount++;
    computerScore.textContent = `Computer: ${computerWinCount}`;
  }
}
//Function to determine winner//
function whoWon() {
  if (playerWinCount > computerWinCount) {
    return `Player wins! Player: ${playerWinCount}, Computer: ${computerWinCount}`;
  } else {
    return `Player loses! Player: ${playerWinCount}, Computer: ${computerWinCount}`;
  }
}
