'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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source