Creating max value on variable

Discussion in 'Learning Javascript' started by Typical, Feb 18, 2019.

  1. Typical

    Typical Villager Member

    Messages:
    7
    Likes Received:
    5
    First Language:
    Polish
    Primarily Uses:
    RMMV
    Hello I have problem that I can't solve. Is there any script call that can make a limit to variable ex. variable 1 is 99 and I add 50 to that variable and it stays 99 ?
     
    #1
  2. Andar

    Andar Veteran Veteran

    Messages:
    28,343
    Likes Received:
    6,450
    Location:
    Germany
    First Language:
    German
    Primarily Uses:
    RMMV
    not directly.
    you need to make your own function for controlling the variable and adding to it. that function would add and then check if the result is higher than the maximum and if yes, set the variable to maximum.
     
    #2
  3. Typical

    Typical Villager Member

    Messages:
    7
    Likes Received:
    5
    First Language:
    Polish
    Primarily Uses:
    RMMV
    @Andar Oh I see. Thanks for help !
     
    #3
  4. Soulrender

    Soulrender Veteran Veteran

    Messages:
    153
    Likes Received:
    79
    First Language:
    Poland
    Primarily Uses:
    RMMV
    I did not tested it, but should work fine
    Code:
    Number.prototype.clamp = function(min, max) {
      return Math.min(Math.max(this, min), max);
    };
    
     
    #4

Share This Page