Skip to content

Limit min/max integer on javascript variable

Im trying to make a talent calculator of sorts, clicking the ‘skill’ will decrease this variable and right-clicking will increase the variable.

The problem i have is i want the variable to be limited from going over 50 or below 0.

Now ive googled and searched here for variable limits and max integers but nothing gives me an idea how to do this, i think im wording my searches wrong.

Could someone give me an idea how to do this or point me in the right direction please, thanks.

Variable im using:

  var a=50;
  function decrease(){a--;document.getElementById('boldstuff').innerHTML= +a;}
  function increase(){a++;document.getElementById('boldstuff').innerHTML= +a;}



In function decrease after a-- add

if (a < 0)
    a = 0;

And in increase:

if (a > 50)
   a = 50;

Or use Math.min(value, minValue) and Math.max(value, maxValue).

User contributions licensed under: CC BY-SA
10 People found this is helpful