It seems the result variable is not defined, what did i do wrong?
In html I called the function using the the onclick attribute, giving both my buttons parameters.
In pickCompterMove() I used math.random to specify heads or tails.
My console does not detect any errors.
const text = document.getElementById('text');
function playGame(playerMove) {
computerMove = pickComputerMove();
let result = '';
if (playerMove === 'heads') {
if (computerMove === 'heads') {
result = 'You win';
} else if (computerMove === 'tails') {
result = 'You lose';
}
} else if (playerMove === 'tails') {
if (computerMove === 'heads') {
result = 'You lose';
} else if (computerMove === 'tails') {
result = 'You win';
}
}
text.innerText = result;
}
It would be nice if you included HTML in the code. Your code may not have worked because the function
pickComputerMove()wasn't stated, but called in the code.In case you want to move out of what you already have and want a shorter line of code, you have the option below, works the same just shorter than the initial code: