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

185
Vistas
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 Respuestas
Responde la pregunta

0

You are calling the method twice

coinTossResult()

assign it to a variable and console.log it

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