Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
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 answers
Answer question

0

You are calling the method twice

coinTossResult()

assign it to a variable and console.log it

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!