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

117
Views
JavaScript Limit Score Increase on click

I'm building a quiz game and I have a working checkAnswer() function and increaseCorrectScore() function. Once the user has clicked a button (one out of three buttons is correct) it will either return correct or wrong then a 'Next' button appears and the user will click to load the nextQuestion() function (function not yet called in code below). My problem: at the moment the user can click on the wrong answer, but can then still click on the correct answer and score a point. They can also keep clicking on the correct answer to cheat in the game. How can I:

  1. stop the user from clicking on the correct answer if they have first clicked on a wrong answer?
  2. stop the user from repeatedly clicking on the correct answer to increase their score before clicking 'Next' for the next question?

I would like to leave the Next button and the functionality that has the user clicking for the next question, rather than simply running nextQuestion() after the first click on an answer.

function checkAnswer() {
    if (this.textContent === newFlags[currentFlagIndex].country) {
        let correct = true
        let correctAnswer = `CORRECT!`
        document.getElementById('result').innerHTML = correctAnswer;
        nextButton.classList.remove('hide');
        setStatusClass(document.body, correct)
        increaseCorrectScore();
    } else {
        let wrong = false
        let wrongAnswer = `WRONG!`
        document.getElementById('result').innerHTML = wrongAnswer;
        nextButton.classList.remove('hide');
        setStatusClass(document.body, wrong)
    }
}
/**
 * Gets the current score from the DOM and increments it by 1
 */
function increaseCorrectScore() {
    let currentScore = parseInt(document.getElementById('correct').innerText);
    document.getElementById('correct').innerText = ++currentScore;
}

The HTML:

<!-- GAME AREA -->
    <div class="container">
        <div id="timer"></div>
        <div class="flag"><img src="" id="flag"></div>
        <div id="answer-buttons" class="answer-box">
            <button class="btn" id="answer-1">Country 1</button>
            <button class="btn" id="answer-2">Country 2</button>
            <button class="btn" id="answer-3">Country 3</button>
        </div>
        <div id="result" class="result"></div>
        <div class="answer-box"><button class="next-btn hide" id="next-question">Next flag >></button></div>
        <div class="score">Score: <span id="correct" class="score">0</span></div>
    </div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Thanks for comments - have decided to hide the buttons until user clicks Next.

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!