Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda