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

196
Visualizações
Same function shows different values at different places

I am making a coin toss application here, everything works well except this part where the coinTossResult() shows tails in resultText.innerHTML, whereas when I log it on to the console in the next line it shows head. Otherwise when coinTossResult() shows tails in resultText.innerHTML it matches with the console.log. No idea why its happening.

resultText.innerHTML = `The Computer chose ${compChoice} and it is a ${(coinTossResult())}`
console.log(compChoice, coinTossResult());

Here is my full code:

    const btnChoices = document.querySelectorAll('.options');
    const resultText = document.querySelector('.result-text');
    const tossWinText = document.querySelector('.toss-winner');
    const turnText = document.querySelector('.turn-text');
    const btnArea = document.querySelector('.buttons');
    let playerChoice =[];
    let compChoice = [];


    function whoseTurn(){
    if(Math.floor(Math.random() * 2) === 0){
        turnText.innerHTML = 'Computer will flip the coin';
        btnArea.style.display = "none";
        const options = ['head', 'tails'];
        const random = Math.floor(Math.random() * options.length);
        compChoice = options[random];
        resultText.innerHTML = `The Computer chose ${compChoice} and it is a ${(coinTossResult())}`
        console.log(compChoice, coinTossResult());
    }else{
        turnText.innerHTML = 'Player will flip the coin';
        btnChoices.forEach(function(e){
            e.addEventListener('click', function(){
                if(e.classList.contains('head')){
                   playerChoice = 'head';
                }else{
                    playerChoice = 'tails';
                }
                resultText.innerHTML = `The Player chose ${playerChoice} and it is a ${coinTossResult()}`
                if(playerChoice === coinTossResult()){
                    tossWinText.innerHTML = 'Player has won the Toss';
                }else{
                    tossWinText.innerHTML = 'Computer has won the Toss';
                }
            })
            
        })
        
        
    }
}
function coinTossResult(){
    const opt = ['head', 'tails'];
    const ran = Math.floor(Math.random() * opt.length);
    const cho = opt[ran];
    return cho;
}
whoseTurn();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are calling the method twice

coinTossResult()

assign it to a variable and console.log it

about 4 years ago · Juan Pablo Isaza Relatório
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