JavaScript How can I execute a function only once? execute function only once javascript,execute function only once jquery,

For example I have a function

Question.prototype.checkAnswer = function(answer1, answer2) {
   if (answer1.innerHTML  == this.correctAnswer) {
    console.log("correct");
    players1.moveCharacter = true;
    pointAmount+= 1;
    point.innerHTML = pointAmount;
  } else {
    console.log("nope")
  }
}

it the answer is correct id adds 1 point to the total amount of points. But the problem is that if I do not move to the next question in array and just keep clicking the answer button, I keep getting points for as much as I want before I decide to move on to the next question. How could I fix this so that only once I can answer the question and get only one point for it. I believe somehow I need to make sure that the function is ran only once?

This is probably can be solved pretty easily but I'm new and can't think of anything.

Answer:1

You can use a simple flag, and set it to true once the function was called.

function Question() {}

Question.prototype.checkAnswer = function(answer1, answer2) {
   
  if(this.answerChecked)
    return; // If answer was already checked, leave.
  
  this.answerChecked = true;
 
  // The rest of your code
  console.log('Run');
}

const question = new Question();

question.checkAnswer('yes', 'no');
question.checkAnswer('again', 'no'); // This will do nothing
Answer:2

Say I have an express route that takes numbers, adds them all up and returns the total. Normally I would do something like this app.get('/add/:num1/:num2/:num3', (req, res) => { // access and ...

Say I have an express route that takes numbers, adds them all up and returns the total. Normally I would do something like this app.get('/add/:num1/:num2/:num3', (req, res) => { // access and ...

const fs = require('fs'); // const util = require('util'); let text = ``; const files = [`head.txt`,`body.txt`,`leg.txt`,`feet.txt`]; let head,body,leg,feet = ``; //const readFile = util.promisify(...

const fs = require('fs'); // const util = require('util'); let text = ``; const files = [`head.txt`,`body.txt`,`leg.txt`,`feet.txt`]; let head,body,leg,feet = ``; //const readFile = util.promisify(...

  1. async/await function in javascript
  2. async await function
  3. async await function javascript
  4. async await function return value
  5. async await function javascript example
  6. async await function nodejs
  7. async await function js
  8. async await function example
  9. async await function react
  10. async await function typescript
  11. async await function with callback
  12. async await function c#
  13. async await function return promise
  14. async await function return
  15. async await function react native
  16. async await function expression
  17. async await function es6
  18. async await function angular
  19. async await function call
  20. async await function angular 6

Is it possible to put JSX inside a template string that is being used as a React render prop? This is what I'm trying to do, but it leads to the link rendering as [object Object] const Container = ({...

Is it possible to put JSX inside a template string that is being used as a React render prop? This is what I'm trying to do, but it leads to the link rendering as [object Object] const Container = ({...

  1. template string within template string

I'm using the Object.assign() to copy values to an object: const { one, two, three } = attributes; return Object.assign( props, { "data-one": one, "data-two": two, "data-three": three } ); ...

I'm using the Object.assign() to copy values to an object: const { one, two, three } = attributes; return Object.assign( props, { "data-one": one, "data-two": two, "data-three": three } ); ...