0

This is a game where the player plays against the computer in a rock paper scissors game. I am trying to make the computer score or player score go up depending on which one wins. But it does not increment when I try to run it. I am new to functions and returning values so I do not really understand it.

// Declaring variables
let playerScore = 0;
let computerScore = 0;


// Gives a random value from the array  
function computerPlay(){
    var things = ['rock', 'paper', 'scissors'];
    var random = things[Math.floor(Math.random()*things.length)];
    console.log('The computer chose: ' + random);
    return random;
}

    // plays a round of the game
function playRound(playerSelection, computerSelection){

    if(playerSelection === computerSelection){
        console.log("tie");
    }
    else if(playerSelection === "rock" && computerSelection === "paper"){
        console.log("YOU LOSE");
        computerWin();
    }
    else if(playerSelection === "rock" && computerSelection === "scissors"){
        console.log("YOU WIN");
        playerWin();   
    }
    else if(playerSelection === "paper" && computerSelection === "rock"){
        console.log("YOU WIN");
        playerWin();
    }
    else if(playerSelection === "paper" && computerSelection === "scissors"){
        console.log("YOU LOSE");
        computerWin();
    }
    else if(playerSelection === "scissors" && computerSelection === "paper"){
        console.log("YOU WIN");
        playerWin();
    }
    else{
        console.log("YOU LOSE");
        computerWin();
    }
}

function playerWin(){
    ++playerScore;
    console.log("Player Score is " + playerScore);
}


function computerWin(){
   ++computerScore;
   console.log("Computer Score is " + computerScore)

}

// Call functions

let chooseWord = "Choose ";
let playerSelection = prompt(chooseWord);
console.log(playerSelection.toLowerCase());
let computerSelection = computerPlay();
computerSelection.toLowerCase();

playRound(playerSelection, computerSelection);
computerWin();
playerWin();
meg
  • 73
  • 6
  • Are you re-running the entire program each time? It might be because `playerScore` and `computerScore` are set to 0 when you initially run the program – byxor Jan 13 '22 at 16:59
  • yes I am re running it in replit everytime – meg Jan 13 '22 at 17:00
  • The state is cleared every time the program exits – byxor Jan 13 '22 at 17:00
  • Do you display game vitals anywhere? In other words do you use any html forms? – akds Jan 13 '22 at 17:01
  • hm I dont know how to make it work then – meg Jan 13 '22 at 17:01
  • no I dont use the html for this but maybe I should? – meg Jan 13 '22 at 17:01
  • If you put `playRound(playerSelection, computerSelection);` in some kind of loop, the program will continue running for multiple rounds and your scores will be maintained – byxor Jan 13 '22 at 17:02
  • Does this answer your question? [How to get JS variable to retain value after page refresh?](https://stackoverflow.com/questions/16206322/how-to-get-js-variable-to-retain-value-after-page-refresh) – Ivar Jan 13 '22 at 17:03
  • I think repl.it evaluates the code server-side so this might not work. Looks like nodejs rather than something running natively in browser – byxor Jan 13 '22 at 17:04
  • yeah I think your right okay I will have to try it on node instead – meg Jan 13 '22 at 17:05
  • It will work in regular JS or node JS, the fundamental issue is that the program exits after 1 round and loses all state. Loops will fix this. Wrapping some code in `while(true) { //codeHere }` will make it repeat indefinitely (or until repl.it kills your program for taking too much time) – byxor Jan 13 '22 at 17:08
  • I dont get loops can you demonstrate how I would use a loop to make it work? – meg Jan 13 '22 at 17:11
  • Try "JavaScript Loops Made Easy" https://youtu.be/Kn06785pkJg – byxor Jan 13 '22 at 17:18

1 Answers1

0

Overall code works well. The improvement to make it a game is to create a loop for a given score (here is to 3 wins).

// Declaring variables
let playerScore = 0;
let computerScore = 0;


// Gives a random value from the array  
function computerPlay(){
    var things = ['rock', 'paper', 'scissors'];
    var random = things[Math.floor(Math.random()*things.length)];
    console.log('The computer chose: ' + random);
    return random;
}

    // plays a round of the game
function playRound(playerSelection, computerSelection){

    if(playerSelection === computerSelection){
        console.log("tie");
    }
    else if(playerSelection === "rock" && computerSelection === "paper"){
        console.log("YOU LOSE");
        computerWin();
    }
    else if(playerSelection === "rock" && computerSelection === "scissors"){
        console.log("YOU WIN");
        playerWin();   
    }
    else if(playerSelection === "paper" && computerSelection === "rock"){
        console.log("YOU WIN");
        playerWin();
    }
    else if(playerSelection === "paper" && computerSelection === "scissors"){
        console.log("YOU LOSE");
        computerWin();
    }
    else if(playerSelection === "scissors" && computerSelection === "paper"){
        console.log("YOU WIN");
        playerWin();
    }
    else{
        console.log("YOU LOSE");
        computerWin();
    }
}

function playerWin(){
    ++playerScore;
    console.log("Player Score is " + playerScore);
}


function computerWin(){
   ++computerScore;
   console.log("Computer Score is " + computerScore)

}

// Call functions
const playToScore = 3
while(playerScore !== playToScore && computerScore !== playToScore) {
    let chooseWord = "Choose ";
    let playerSelection = prompt(chooseWord);
    console.log(playerSelection.toLowerCase());
    let computerSelection = computerPlay();
    computerSelection.toLowerCase();

    playRound(playerSelection, computerSelection);
    console.log('Results are: Player -', playerScore, 'Computer -', computerScore)
}

if (playerScore === playToScore) {
    console.log('Player won with score', playerScore)
} else if (computerScore === playToScore) {
    console.log('Computer won with score', computerScore)
}