-1

I've set a minimum of 60 for Strength in this tool, how can I set a maximum number? When the tool is complete there will be more than just "Strength" so you know.

It was suggested that I store the limits as two variables, can anyone provide an example of this? I am new to JS.

CODE & DEMO

tshepang
  • 12,111
  • 21
  • 91
  • 136
user2811882
  • 67
  • 1
  • 2
  • 8
  • What does this question have to do with google? In fact, what _does_ this question have to do with? Very few people are going to click your link to read your code. You must post the _relevant_ code here. – John Saunders Oct 13 '13 at 01:40

2 Answers2

1

You can store the maximum and minimum as two properties of each statistic limit, as such:

Limits: {
    Strength: {
        max: 70,
        min: 60
    }
}

Then, do basically the same thing you are doing to check for the minimum, again for the maximum (in your add function).

if(newNumber > character.Limits[stat].max) return;

Working demo

Also, unrelated, but note that it is a Javascript naming conventions to have the object properties use lowercase names. In general, all variables except for globals and object constructors start with a lowercase letter.See here

Sunyatasattva
  • 5,619
  • 3
  • 27
  • 37
  • 1
    yes.. add commas ater the end }.. var Alexander = { Strength: "AlexanderStrengthVal", Stamina: "AlexanderStaminaVal", Bonus: "AlexanderRemainingBonusVal", Limits: { Strength: { max: 70, min: 60 }, Stamina: { max: 70, min: 60, }, } }; – X-Pippes Oct 12 '13 at 23:48
  • 2
    You are missing a comma after the closing bracket of `Strength` – Sunyatasattva Oct 12 '13 at 23:48
  • @Sunyatasattva I have a new question [**here**](http://stackoverflow.com/questions/19409507/incorporating-onmouseup-down-functionality) that I wanted your opinion on. All of your answers have been perfect for me thus far. Thanks! – user2811882 Oct 16 '13 at 19:23
0

First define your var

var maxStrength = 65. 

Then define your condition in add funcion

function add(character, stat)
{

  var txtNumber = document.getElementById(character[stat]);
  var newNumber = parseInt(txtNumber.value) + 1;
  var BonusVal = document.getElementById(character["Bonus"]);
  if(parseInt(txtNumber.value) < maxStrength) {
    if(BonusVal.value == 0) return;
    var newBonus = parseInt(BonusVal.value) - 1;
    BonusVal.value = newBonus; 
    txtNumber.value = newNumber;
  }
}

In this way, it only adds new strength until reach the maximum,

X-Pippes
  • 1,170
  • 7
  • 25