Rock Paper Scissors game in console (Odin Project)

Viewed 46

I am a beginner JS programmer and I am having issues finishing a JavaScript rock paper scissors game for the Odin Project. for now, I can get the game to run for 5 rounds but it does not recall the player selection function it keeps the first selection and runs over 5 times on it. Also, some undefined variables pop up in the last line. and I'm unable to figure it out. and can you tell me any issues with my code if any to make it better?? Thank you in demand!

Here is my code:

// get computer choice
function getComputerChoice() {
  let choices = ["rock", "paper", "scissors"];
  let computerChoice = choices.at(Math.floor(Math.random() * choices.length));
  return computerChoice;
}

// get player choice
function getPlayerChoice() {
  let playerInput = prompt("type your choice...");
  let result = playerInput.toLowerCase();
  return result;
}

// play one round and save the score
function playRound(playerSelection, computerSelection) {
  if (playerSelection === computerSelection) {
    return "it's a tie replay this round";
  } else if (playerSelection === "rock" && computerSelection === "paper") {
    return `You lose! paper beats rock`;
  } else if (playerSelection === "rock" && computerSelection === "scissors") {
    return `You win! rock beats scissors`;
  } else if (playerSelection === "paper" && computerSelection === "scissors") {
    return `You lose!scissors beats paper`;
  } else if (playerSelection === "paper" && computerSelection === "rock") {
    return `You win! paper beats rock`;
  } else if (playerSelection === "scissors" && computerSelection === "rock") {
    return `You lose! rock beats scissors`;
  } else if (playerSelection === "scissors" && computerSelection === "paper") {
    return `You win! scissors beats paper`;
  }
}

const playerSelection = getPlayerChoice();
const computerSelection = getComputerChoice();
console.log(playRound(playerSelection, computerSelection));

function game() {
  let computerScore = 0,
    playerScore = 0;

  for (i = 0; i < 3; i++) {
    const result = playRound(playerSelection, computerSelection);
    if (result.includes("win")) {
      playerScore++;
      console.log(`computer: ${computerScore} | player: ${playerScore}`);
    } else if (result.includes("lose")) {
      computerScore++;
      console.log(`computer: ${computerScore} | player: ${playerScore}`);
    }
    console.log(
      "Final Results: Player: " + playerScore + " Computer: " + computerScore
    );
    if (playerScore > computerScore) {
      console.log("You win the game!");
    } else if (playerScore < computerScore) {
      console.log("You lose the game.");
    } else {
      console.log("The game was an overall tie.");
    }
  }
}

game();

1 Answers

You are very close! First of, you run getPlayerChoice and getComputerChoice outside the loop, so it does not change afterwards. I've moved it when you call the round.

Also, you were checking the winner of the 3 games before the 3 games finished, so I moved that part outside of the loop.

    // get computer choice
function getComputerChoice() {
  let choices = ["rock", "paper", "scissors"];
  let computerChoice = choices.at(Math.floor(Math.random() * choices.length));
  return computerChoice;
}

// get player choice
function getPlayerChoice() {
  let playerInput = prompt("type your choice...");
  let result = playerInput.toLowerCase();
  return result;
}

// play one round and save the score
function playRound(playerSelection, computerSelection) {
  if (playerSelection === computerSelection) {
    return "it's a tie replay this round";
  } else if (playerSelection === "rock" && computerSelection === "paper") {
    return `You lose! paper beats rock`;
  } else if (playerSelection === "rock" && computerSelection === "scissors") {
    return `You win! rock beats scissors`;
  } else if (playerSelection === "paper" && computerSelection === "scissors") {
    return `You lose!scissors beats paper`;
  } else if (playerSelection === "paper" && computerSelection === "rock") {
    return `You win! paper beats rock`;
  } else if (playerSelection === "scissors" && computerSelection === "rock") {
    return `You lose! rock beats scissors`;
  } else if (playerSelection === "scissors" && computerSelection === "paper") {
    return `You win! scissors beats paper`;
  }
}

function game() {
  let computerScore = 0,
    playerScore = 0;

  for (i = 0; i < 3; i++) {
    const result = playRound(getPlayerChoice(), getComputerChoice());
    console.log(result);
    if (result.includes("win")) {
      playerScore++;
      console.log(`computer: ${computerScore} | player: ${playerScore}`);
    } else if (result.includes("lose")) {
      computerScore++;
      console.log(`computer: ${computerScore} | player: ${playerScore}`);
    }
    
  }
  console.log(
      "Final Results: Player: " + playerScore + " Computer: " + computerScore
    );
    if (playerScore > computerScore) {
      console.log("You win the game!");
    } else if (playerScore < computerScore) {
      console.log("You lose the game.");
    } else {
      console.log("The game was an overall tie.");
    }
}

game();

Related