Cant' get my code for Rock, PaperScissors to work


#1

Any assistance would be super helpful. I am getting nothing from the ComputerChoice variable, my game ends in a tie everytime regardless of input.

indent preformatted text by 4 space
<html>
<head>
</head>
<body>
<script>

function chooseRandom(){
    return (Math.floor(Math.random()*3)+1);
}

let computerSelection = chooseRandom();
console.log(computerSelection)

function computerPlay(){

    if(computerSelection == 1){
        return 'Rock';

        } else if(computerSelection == 2){
        return 'Paper';

        } else if (computerSelection == 3){
        return 'Scissors';

        }
    let compCase = computerSelection.toUpperCase();
    console.log(compCase)
    return compCase;
}

function playerSelection(){
    let playerPick = prompt("Choose Rock, Paper or Scissors.")
    let playCase = playerPick.toUpperCase();
    console.log(playCase)
    return playCase;
}


let playerChoice = playerSelection();
let computerChoice = computerPlay();

function playRound(playerChoice, computerChoice){

    if(playerChoice == computerChoice){
        return "Its a tie! Darn it.";

    }else if(playerChoice == "ROCK" && computerChoice == "SCISSORS"){
        return "You Win! Rock beats Scissors.";

    } else if (playerChoice == "PAPER" && computerChoice == "ROCK"){
        return "You Win! Paper beats Rock.";

    } else if (playerChoice == "SCISSORS" && computerChoice == "PAPER"){
        return "You Win! Scissors beats Paper.";

    } else if (computerChoice == "ROCK" && playerChoice == "SCISSORS"){
        return "You lose! Rock beats Scissors.";

    } else if (computerChoice == "PAPER" && playerChoice == "ROCK"){
        return "You lose! Rock beast Paper.";

    } else if (computerChoice == "SCISSORS" && playerChoice == "PAPER"){
        return "You lose! Scissors beats Paper.";

    }
}
console.log(playRound())
</script>
</body>
</html>

#2

Two things: first, you’ll want to move this line

let computerSelection = chooseRandom();

into the computerPlay function like so:

function computerPlay() {
  let computerSelection = chooseRandom();
  // leave the rest of this function the same
}

Otherwise, your computer will make the same move every time.

===

Second, the reason you are getting a tie every time is because you aren’t passing in your playerChoice and computerChoice variables into your playRound function! Try changing

console.log(playRound());

To

console.log(playRound(playerChoice, computerChoice));

And see if that works!


#3

Thank you for the help. I am now stuck on the game(); function. I dont understand why i cannot change the player’s choice after the first choice. And i cant install the variables for the choices inside of the functions because it makes everything screwy. at least right now I am getting the game to run I just cant change the choices. Is it common practice to have to write functions and variables just to get basic code working and then have to rework everything just to get to the next step?

<html>
<head>
</head>
<body>
<script>
///get number 1-3 to represent computer's decision in play function
function chooseRandom(){
    return (Math.floor(Math.random()*3)+1);
}

///convert number into computer choice
function computerPlay(){

    let computerSelection = chooseRandom();

    if(computerSelection == 1){
        return "Rock";

        } else if(computerSelection == 2){
        return "Paper";

        } else if (computerSelection == 3){
        return "Scissors";

        }
}
///convert computer pick into upper case
function compCase(){
    let compPick = computerPlay();
    let compCase = compPick.toUpperCase();
    return compCase;
}

///player prompted for thier choice and converted to uppercase lettering
function playerSelection(){
    let playerPick = prompt("Choose Rock, Paper or Scissors.")
    let playCase = playerPick.toUpperCase();
    return playCase;
}

///set choices equal to corresponding functions
let playerChoice = playerSelection();
let computerChoice = compCase();

///variables to keep score
let playerScore = 0;
let computerScore = 0;

///play one round of RPS
function playRound(playerChoice, computerChoice){


    if(playerChoice == computerChoice){
        return "Its a tie! Darn it.";

    }else if(playerChoice == "ROCK" && computerChoice == "SCISSORS"){
        playerScore++;
        return "You Win! Rock beats Scissors.";
        

    } else if (playerChoice == "PAPER" && computerChoice == "ROCK"){
        playerScore++;
        return "You Win! Paper beats Rock.";
        

    } else if (playerChoice == "SCISSORS" && computerChoice == "PAPER"){
        playerScore++;
        return "You Win! Scissors beats Paper.";
        

    } else if (computerChoice == "ROCK" && playerChoice == "SCISSORS"){
        computerScore++;
        return "You lose! Rock beats Scissors.";
        

    } else if (computerChoice == "PAPER" && playerChoice == "ROCK"){
        computerScore++;
        return "You lose! Rock beast Paper.";
        

    } else if (computerChoice == "SCISSORS" && playerChoice == "PAPER"){
        computerScore++;
        return "You lose! Scissors beats Paper.";
        

    }
}

function game(){

    playRound(playerChoice, computerChoice);
    console.log(playRound(playerChoice, computerChoice));
        console.log(playerScore, computerScore);

    playRound(playerChoice, computerChoice);
    console.log(playRound(playerChoice, computerChoice));
        console.log(playerScore, computerScore);

    playRound(playerChoice, computerChoice);
        console.log(playerScore, computerScore);

    playRound(playerChoice, computerChoice);
        console.log(playerScore, computerScore);
        
    playRound(playerChoice, computerChoice);
        console.log(playerScore, computerScore);
}

game();


</script>
</body>
</html>

#4

The reason you can’t change the player’s choice after the first choice is because you are only setting it once with this line:

let playerChoice = playerSelection();

The playerSelection (and compCase) function should be ran each time you want to get a new selection. Your current code can achieve this by moving those variables into the function game and changing it to look like this:

function game() {
  let playerChoice = playerSelection();
  let computerChoice = compCase();

  // round 1
  playRound(playerChoice, computerChoice);
  console.log(playerScore, computerScore);

  // round 2
  playerChoice = playerSelection();
  computerChoice = compCase();
  playRound(playerChoice, computerChoice);
  console.log(playerScore, computerScore);

  // round 3
  playerChoice = playerSelection();
  computerChoice = compCase();
  playRound(playerChoice, computerChoice);
  console.log(playerScore, computerScore);
}

However, this, and many other areas of your code can benefit from removing the variables altogether, like so:

function game() {
  // round 1
  playRound(playerSelection(), compCase());
  console.log(playerScore, computerScore);

  // round 2
  playRound(playerSelection(), compCase());
  console.log(playerScore, computerScore);

  // round 3
  playRound(playerSelection(), compCase());
  console.log(playerScore, computerScore);
}

That should, hopefully, get your code working!


Is it common practice to have to write functions and variables just to get basic code working and then have to rework everything just to get to the next step?

Sadly, yes! However, as you spend more time programming, you will begin to get better about these kinds of things, and start to get a feel for what should and should not be broken out into functions, that will make this whole process quite a bit less painful. I’ll give you a couple examples based on the code you have posted here.

///convert number into computer choice
function computerPlay(){

    let computerSelection = chooseRandom();

    if(computerSelection == 1){
        return "Rock";

        } else if(computerSelection == 2){
        return "Paper";

        } else if (computerSelection == 3){
        return "Scissors";

        }
}
///convert computer pick into upper case
function compCase(){
    let compPick = computerPlay();
    let compCase = compPick.toUpperCase();
    return compCase;
}

Can be converted into one function quite simply:

function getComputerChoice() {
  let computerSelection = chooseRandom();
  
  // generally, you will want to use === instead of == for number comparison
  if (computerSelection === 1) return "ROCK";
  if (computerSelection === 2) return "PAPER";
  if (computerSelection === 3) return "SCISSORS";
}

Another example is your playerSelection function, which can be rewritten as

function playerSelection() {
  return prompt("Choose Rock, Paper, or Scissors.").toUpperCase();
}

// or, using an arrow function:
const playerSelection = () => (prompt("Choose, Rock, Paper, or Scissors.").toUpperCase());