Home > Net >  Rock-Paper-Scissors computerSelection not updating in loop Javascript
Rock-Paper-Scissors computerSelection not updating in loop Javascript

Time:01-28

I am writing this simple console game Rock-Paper-Scissors for TheOdinProject course

I'm having a problem with with the computerSelection not being updated in loop.

There should be 5 rounds in which a player enters rock/paper/scissors in the prompt window and it gets compared against randomized computer answer.

The overall logic of the game seems to be working fine but the problem is that computerSelection stays the same every turn once generated in round 1 but the prompt updates playerSelection as it should.

Here's the code:

const rock = 'rock';
const paper = 'paper';
const scissors = 'scissors';

let playerSelection = '';
const computerSelection = getComputerChoice();

function getComputerChoice() {
    let items = [rock, paper, scissors];
    let random = Math.floor(Math.random() * 3);
    let choice = items[random];
    return choice;
}

function playRound(playerSelection, computerSelection) {
    if (playerSelection == computerSelection) {
        roundResult = "It's a Draw";
    } else if (playerSelection == rock && computerSelection == paper) {
        roundResult = "Computer wins this round! Paper beats rock!";
    } else if (playerSelection == rock && computerSelection == scissors) {
        roundResult = "Human wins this round! Rock beats scissors!";

    } else if (playerSelection == paper && computerSelection == rock) {
        roundResult = "Human wins this round! Paper beats rock!"
    } else if (playerSelection == paper && computerSelection == scissors) {
        roundResult = "Computer wins this round! Scissors beats paper";
    
    } else if (playerSelection == scissors && computerSelection == rock) {
        roundResult = "Computer wins this round! Rock beats scissors!";
    } else if (playerSelection == scissors && computerSelection == paper) {
        roundResult = "Human wins this round! Scissors beats paper!";
    }
    return roundResult;
}

function game() {
    for (let i = 0; i < 5; i  ) {
        playRound(prompt("What do you choose?"), computerSelection);
        console.log(roundResult);
    }
    
}

onsole.log("Machine: "   computerSelection);
console.log(game());

CodePudding user response:

Pass the getComputerChoice() in the playground args.

CodePudding user response:

as @Barmar suggested, all I had to do was put the

const computerSelection = getComputerChoice(); 

inside the loop.

const rock = 'rock';
const paper = 'paper';
const scissors = 'scissors';

let playerSelection = '';
const computerSelection = getComputerChoice();

function getComputerChoice() {
    let items = [rock, paper, scissors];
    let random = Math.floor(Math.random() * 3);
    let choice = items[random];
    return choice;
}

function playRound(playerSelection, computerSelection) {
    if (playerSelection == computerSelection) {
        roundResult = "It's a Draw";
    } else if (playerSelection == rock && computerSelection == paper) {
        roundResult = "Computer wins this round! Paper beats rock!";
    } else if (playerSelection == rock && computerSelection == scissors) {
        roundResult = "Human wins this round! Rock beats scissors!";

    } else if (playerSelection == paper && computerSelection == rock) {
        roundResult = "Human wins this round! Paper beats rock!"
    } else if (playerSelection == paper && computerSelection == scissors) {
        roundResult = "Computer wins this round! Scissors beats paper";
    
    } else if (playerSelection == scissors && computerSelection == rock) {
        roundResult = "Computer wins this round! Rock beats scissors!";
    } else if (playerSelection == scissors && computerSelection == paper) {
        roundResult = "Human wins this round! Scissors beats paper!";
    }
    return roundResult;
}

function game() {
    for (let i = 0; i < 5; i  ) {
        const computerSelection = getComputerChoice();
        playRound(prompt("What do you choose?"), computerSelection);
        console.log(roundResult);
    }
    
}

console.log("Machine: "   computerSelection);
game();

  • Related