简体   繁体   中英

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.

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM