0

I don't know what I'm doing wrong but I have problem with counting in javascript probably I missed something stupid and small but just cant find where.

So I want value of LGtv to increase on button click by 0.01 every time is clicked, at the same time I want value of CC to decrease on buttonClick by -1.

When I click first time all works, but when I clicked second time I'm getting message "You are a WINNER!" but LGtv value just increase by 0.01 so its not more than 0.05 like in my first If condition.

    <span id="currentclicks" class="btn btn-primary"></span>

    <div class="control-group">

    <label id="TvDiv" class="control-label"><span>0.00</span>
    </label>



    <div id="hm">
    <span id="TvButton" class="btn btn-large btn-primary pull-right"> Click </span>
    </div>

var LGtv = 0.00;
var CC = 10;

document.getElementById('currentclicks').innerHTML = CC;
document.getElementById('TvButton').onclick = function() {
  if (LGtv < 0.05) {
    document.getElementById('TvDiv').innerHTML = LGtv +=0.01;
  } else {
    document.getElementById('TvDiv').innerHTML = "You are a WINNER!";
    document.getElementById('hm').innerHTML = "";
  }
  localStorage.setItem('TvDiv', LGtv);

  if (LGtv++) {
    document.getElementById('currentclicks').innerHTML = CC -= 1;
  }
}

Thanks all for help!

Arek Marcjanik
  • 105
  • 1
  • 7

1 Answers1

-1

You should move if (LGtv++) { into the if where you are updating LGtv.

var LGtv = 0.00;
var CC = 10;

document.getElementById('currentclicks').innerHTML = CC;
document.getElementById('TvButton').onclick = function() {
  if (LGtv < 0.05) {
    document.getElementById('TvDiv').innerHTML = (LGtv += 0.01);
    document.getElementById('currentclicks').innerHTML = (CC -= 1);
  } else {
    document.getElementById('TvDiv').innerHTML = "You are a WINNER!";
    document.getElementById('hm').innerHTML = "";
  }
  localStorage.setItem('TvDiv', LGtv);    
}
Brandon Griffin
  • 348
  • 1
  • 8