Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

html - function for a value to go up +0.01 when called in javascript not working (beginner)

function startGame(){
  let currentpoints = 0.01;
  var writepoints = document.
  getElementById("score").innerHTML = currentpoints += 0.01;
  writepoints;
}

This function is called by a button that you press, and its purpose is simple, it displays currentpoints to an html element by ID after increasing its value. The code will run with no errors and go up from 0.01 - to 0.02, but it stops there. it remains 0.02 and does not proceed to go up.

My theory is when the function is called, the declaration of currentpoints is set again to its 0.01 value , but I don't want that, I want it to proceed to go up every time you press the button (call the function), so can someone tell me my remedy to this solution?

Thank you guys, I am a beginner trying to get involved in a way that is fun and rememberable for me. (simple mini game) New here by the way to stackoverflow.

question from:https://stackoverflow.com/questions/65932372/function-for-a-value-to-go-up-0-01-when-called-in-javascript-not-working-begin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

currentpoints gets reset to 0.01 each time the user presses the button and startGame is called. You need to remember it outside the function and use that.

Incidentally, what is the purpose of the writepoints on the last line of the function?

   let currentpoints = 0.01;
   function startGame(){
      var writepoints = document.
      getElementById("score").innerHTML = currentpoints += 0.01;
      writepoints;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...