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

119
Vistas
Cannot get my for loop counter to work in JS. Any help would be appreciated

I am trying to build a simple rock/paper/scissors game with JS. I am having trouble accumulating wins/losses. The game is working fine but for some reason wins losses are staying at 0 and are not adding at all. As you can see in the bellow code I have declared variables for wins/losses/draws and added wins++, losses++, draws++ in the appropriate cases. I added console log in the loop to be able to watch them update as I go through the game, however I am not sure why they are staying at 0. Any help would be greatly appreciated. Thank you

    const options = [
      "Rock",
      "Paper",
      "Scissors"
    ];

    let wins = 0;
    let losses = 0;
    let draws = 0;

    function computerPlay() {
        var computerSelection = options[Math.floor(Math.random()*options.length)];
        return computerSelection;
      }

    function playRound(playerSelection, computerSelection) {
      
      computerSelection = computerPlay().toLowerCase();
      if (computerSelection === "rock" && playerSelection === "paper") {
        return("You win! Paper beats rock");
        wins++;
      }
      else if (computerSelection === "scissors" && playerSelection === "paper") {
        return("You lose! Scissors beats paper");
        losses++;
      }
      else if (computerSelection === "scissors" && playerSelection === "rock") {
        return("You win! Rock beats scissors");
        wins++;
      }
      else if (computerSelection === "paper" && playerSelection === "rock") {
        return("You lose! Paper beats rock");
        losses++;
      }
      else if (computerSelection === "rock" && playerSelection === "scissors") {
        return("You lose! Rock beats scissors");
        losses++;
      }
      else if (computerSelection === "paper" && playerSelection === "scissors") {
        return("You win! Scissors beats paper");
        wins++;
      }
      else  if (computerSelection === playerSelection){
        return("Draw!");
        draws++;
      }
      else {
        return("Invalid Input!")
      }
    }

    function game() {
      
      let playerSelection
      
      for(r = 1; r <= 5; r++) {
        let computerSelection = computerPlay().toLowerCase()
        playerSelection = prompt("rock, paper, or scissors?").toLowerCase();
        result = playRound(playerSelection, computerSelection);
        console.log(result);
        console.log("wins: " + wins);
        console.log("losses: " + losses);
        console.log("draw: " + draws);
      }
      
    }

    game();
about 4 years ago · Juan Pablo Isaza
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