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

220
Vistas
How to compare the ID of a clicked button to another string?

I am trying to compare a clicked button's ID to a random string output from another function and log the result in the console (it's a rock, paper, scissors game); Code depicted below


buttons.forEach((button) => {
    button.addEventListener("click", () => {
        console.log(playRound(event.srcElement.id));
    });
});

function playRound(playerSelection) {
    const computerSelection = computerPlay;
    if (playerSelection === 'rock' && computerSelection === 'rock') {
        return 'Tie Game!';
    } else if (playerSelection === 'rock' && computerSelection === 'paper') {
        return 'Paper beats rock! You lose!';
    } else if (playerSelection === 'rock' && computerSelection === 'scissors') {
        return 'Rock beats scissors! You win!';
    } else if (playerSelection === 'paper' && computerSelection === 'rock') {
        return 'Paper beats rock! You win!';
    } else if (playerSelection === 'paper' && computerSelection === 'paper') {
        return 'Tie Game!';
    } else if (playerSelection === 'paper' && computerSelection === 'scissors') {
        return 'Scissors beats paper! You lose!';
    } else if (playerSelection === 'scissors' && computerSelection === 'rock') {
        return 'Rock beats Scissors! You lose!';
    } else if (playerSelection === 'scissors' && computerSelection === 'paper') {
        return 'Scissors beats paper! You win!';
    } else if (playerSelection === 'scissors' && computerSelection === 'scissors') {
        return 'Tie Game!';
    }
}

function computerPlay() {
    const play = (Math.floor(Math.random() * 3));
    if (play === 0) {
        return 'rock';
    }

    else if (play === 1) {
        return 'paper';
    }

    else if (play === 2) {
        return 'scissors';
    }
}

I know this code is a bit inefficient but my problem is that the playRound() function returns undefined when I execute this. I've tried logging the button's ID alone, which worked, I've tried passing the button's ID as a parameter to a dummy function, which worked, and I tried logging the computerPlay() output to make sure it was returning a string (it was). I can't tell why my playRound() function is unable to compare the two strings given and return a result string.

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