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

198
Views
Rock, Paper, Scissors made with switch statement

I'm trying to develop my first game with Js, the issue is that my scores and round variables are not getting summed up, when I console log them they don't show like this: playerScore = 2, computerScore = 1, round = 2, here's my code.

    // Computer random selection 
function computerPlay() {
    //Game options to play with 
    let options = ['Rock', 'Paper', 'Scissors'];
    //Random number generator
    let randomNumber = Math.floor(Math.random() * 3);
    //Select random option from array
    let computerSelection = options[randomNumber];
    // return value so the game can be played
    return computerSelection;
}

let computerSelection = computerPlay();

let playerSelection = computerPlay();

//Round
let round = 1;

//Add round
function addRound () {
    round += 1;
}

//Scores
let playerScore = 0;
let computerScore = 0;

// Start round, define winner, loser or tie game 
function playRound (playerSelection, computerSelection) {
    //concatenate strings to match cases 
    switch (playerSelection + computerSelection) {
        // Tie case
        case 'RockRock':
        case 'PaperPaper':
        case 'ScissorsScissors':
            playerScore += 0;
            computerScore += 0;
            return 'Tie Game!'; 
        // Player score
        case 'RockScissors':
        case 'PaperRock':
        case 'ScissorsPaper':
            playerScore += 1;
            return `${playerSelection} beats ${computerSelection}, You Won!`;
        // Computer score    
        case 'ScissorsRock':
        case 'RockPaper':
        case 'PaperScissors':
            computerScore += 1;
            return `${computerSelection} beats ${playerSelection}, Computer Won :(`;
    }
    addRound();
}
    
console.log(playRound(playerSelection, computerSelection));

console.log(playerScore, computerScore);

This is the output of the console:

    Scissors beats Paper, You Won!
    1 0

    Tie Game!
    0 0
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are using return in swich-case, which will not execute the code below the return statement. And addRound() function is below the return statements. By moving it on upper side will do the work

const computerPlay = () => {
  const options = ['Rock', 'Paper', 'Scissors'];
  const randomNumber = Math.floor(Math.random() * 3);
  const computerSelection = options[randomNumber];
  return computerSelection;
}

let round = 0;

let addRound = () => round += 1;

let playerScore = 0;
let computerScore = 0;

function playRound() {
  let computerSelection = computerPlay();
  let playerSelection = computerPlay();

  addRound();

  switch (playerSelection + computerSelection) {
    case 'RockRock':
    case 'PaperPaper':
    case 'ScissorsScissors':
      playerScore = 0;
      computerScore = 0;
      return 'Tie Game!';
    case 'RockScissors':
    case 'PaperRock':
    case 'ScissorsPaper':
      playerScore += 1;
      return `${playerSelection} beats ${computerSelection}, You Won!`;
    case 'ScissorsRock':
    case 'RockPaper':
    case 'PaperScissors':
      computerScore += 1;
      return `${computerSelection} beats ${playerSelection}, Computer Won :(`;
  }
}

console.log(playRound());
console.log(playerScore, computerScore, round);

console.log(playRound());
console.log(playerScore, computerScore, round);

about 4 years ago · Santiago Trujillo 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!