Search code examples
javascripthtmlcsshtml-helper

javascript game help pls


The game is able to get unlimited money by pressing the button. How do I prevent this?

var workbard = document.getElementById("workbar");
function work() {
  var zaman = setInterval(function () {
    workbard.value +=1;
    if (workbard.value == 10) {
      workbard.value -=11;
      workbard.style.visibility = 'hidden'
      clearInterval(zaman);
      money = money +=5;
      experience = experience +=10;
      document.getElementById("para").innerHTML= ":" +money;
      document.getElementById("exp").innerHTML=":" +experience;
    }
<progress id="workbar" alue="0" max="10"></progress>
<button type ="button"id="workb2"onclick="work(),document.getElementById('workbar').style.visibility='visible'">Work</button>


Solution

  • you need to initialize some variables, add a limit and then use an if statement

    var workbard = document.getElementById("workbar");
    var money = 0;
    var experience=0;
    var limit = 30;
    function work() {
      var zaman = setInterval(function () {
        workbard.value +=1;
        if (workbard.value == 10) {
          workbard.value -=11;
          workbard.style.visibility = 'hidden'
          clearInterval(zaman);
          money = money +=5;
          if(money > limit)money = limit;
          experience = experience +=10;
          document.getElementById("para").innerHTML= ":" +money;
          document.getElementById("exp").innerHTML=":" +experience;
        }})}
    <progress id="workbar" alue="0" max="10"></progress>
    <button type ="button"id="workb2"onclick="work(),document.getElementById('workbar').style.visibility='visible'">Work</button>
    
    <div id='para'></div>
    <div id='exp'></div>