JS CLI RPS game issue

Viewed 42

Greetings to the caring people of Stack Overflow. I just recently started to learn JS and am now trying to make a CLI Rock Paper Scissors game. Yes, I know that I am the 100th person who has asked questions about it, but I could not find the 'no-make-sense' code through the code of other people. So just I ask for help from everyone who is not indifferent to helping a newbie!

The situation is this: the most common rules of a familiar game, but I can't get the counter to work, plus sometimes the text I type in is not recognized (I think the reason for the input comparison function). Also, I'm sure that my func scopes are broken but where and why I can't figure out or trace it.

function computerPlay() {
  let anyPick = Math.random();
  if (anyPick < 0.3) {
    return "Rock";
  }
  if (anyPick > 0.3 && anyPick < 0.6) {
    return "Paper";
  }
  if (anyPick > 0.6) {
    return "Scissors";
  }
}

let aiScore = 0;
let playerScore = 0;
function battleRound(playerSelection, computerSelection) {
  if (playerSelection === computerSelection) {
    return "None of you are win or lose, cuz it is an equal!";
  }
  if (playerSelection === "rock" && computerSelection === "Scissors") {
    playerScore += 1;
    return "You Win! Rock beats Scissors";
  }
  if (playerSelection === "rock" && computerSelection === "Paper") {
    aiScore += 1;
    return "You Loose! Paper beats Rock";
  }
  if (playerSelection === "paper" && computerSelection === "Scissors") {
    aiScore += 1;
    return "You Loose! Scissors cut Paper";
  }
  if (playerSelection === "paper" && computerSelection === "Rock") {
    playerScore += 1;
    return "You Win! Paper beats Rock";
  }
  if (playerSelection === "scissors" && computerSelection === "Paper") {
    playerScore += 1;
    return "You Win! Scissors cut Paper";
  }
  if (playerSelection === "scissors" && computerSelection === "Rock") {
    aiScore += 1;
    return "You Loose! Rock beats Scissors";
  } else return "U misspelled, try again";
}

function scores(aiScore, playerScore) {
  if (aiScore > playerScore) {
    return "CONGRAS, AI IS SMARTER THEN YOU!";
  }
  if (playerScore > aiScore) {
    return "CONGRAS, YOU ARE SMARTER THEN AI";
  }
}

for (let i = 0; i < 5; i++) {
  const playerSelection = prompt(
    "Choose: Rock, Paper or Scissors",
    " "
  ).toLowerCase();
  const computerSelection = computerPlay();
  console.log("AI choose: " + computerSelection);
  console.log(battleRound(playerSelection, computerSelection));
  console.log("AI score: " + aiScore, "Player score: " + playerScore);
}

Please, indicate an obvious problem in the code and what exactly I need to replace. Much obliged for any tips!

1 Answers

There are two main problems with your code that might be causing this issue,

  • You are setting the default prompt value to an empty space. By using:
prompt("Choose: Rock, Paper or Scissors", " ")

The default prompt value is an empty space, making it easier to mistype the value.

To fix this, I removed the default value and added the .trim() method to remove whitespace from the start and end of the input.

  • You are making impossible comparisons. In your code you made the comparison:
if (playerSelection === computerSelection) {
     return ("None of you are win or lose, cuz it is an equal!");
}

This condition will never be true, as the playerSelection is always converted to lowercase e.g "rock", but the computerSelection's first letter is always uppercase e.g "Rock".

I added the .toLowerCase() method to the end of computerSelection before comparing.

Full code:

function computerPlay() {
  let anyPick = Math.random();
  if (anyPick < 0.3) {
    return "Rock";
  }
  if (anyPick > 0.3 && anyPick < 0.6) {
    return "Paper";
  }
  if (anyPick > 0.6) {
    return "Scissors";
  }
}

let aiScore = 0;
let playerScore = 0;

function battleRound(playerSelection, computerSelection) {
  if (playerSelection === computerSelection.toLowerCase()) {
    return "None of you are win or lose, cuz it is an equal!";
  }
  if (playerSelection === "rock" && computerSelection === "Scissors") {
    playerScore += 1;
    return "You Win! Rock beats Scissors";
  }
  if (playerSelection === "rock" && computerSelection === "Paper") {
    aiScore += 1;
    return "You Loose! Paper beats Rock";
  }
  if (playerSelection === "paper" && computerSelection === "Scissors") {
    aiScore += 1;
    return "You Loose! Scissors cut Paper";
  }
  if (playerSelection === "paper" && computerSelection === "Rock") {
    playerScore += 1;
    return "You Win! Paper beats Rock";
  }
  if (playerSelection === "scissors" && computerSelection === "Paper") {
    playerScore += 1;
    return "You Win! Scissors cut Paper";
  }
  if (playerSelection === "scissors" && computerSelection === "Rock") {
    aiScore += 1;
    return "You Loose! Rock beats Scissors";
  } else return "U misspelled, try again";
}

function finalScores() {
  if (aiScore > playerScore) {
    return "CONGRATS, AI IS SMARTER THEN YOU!";
  }
  if (playerScore > aiScore) {
    return "CONGRATS, YOU ARE SMARTER THEN AI";
  }
}

for (let i = 0; i < 5; i++) {
  const playerSelection = (prompt("Choose: Rock, Paper or Scissors") ?? "")
    .trim()
    .toLowerCase();
  const computerSelection = computerPlay();
  console.log("AI choose: " + computerSelection);
  console.log(battleRound(playerSelection, computerSelection));
  console.log("AI score: " + aiScore, "Player score: " + playerScore);
}

console.log(finalScores())

Related