Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
Why won't my variable increment everytime I run the program?

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();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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)
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!