Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda