“< = 0”不预防否定?

时间:2013-11-13 16:53:34

标签: javascript return-value negative-number

查看:

-WORKING EXAMPLE-

如何复制我的*问题:

如果您将所有奖励积分放入除1之外的统计数据,然后按CTRL-CLICK中的其他统计数据,那么奖励值将进入负数,当 *它应该停在0

控制:

  • 左键单击=分配1点
  • CTRL左键单击​​=分配5分
  • 右键单击=取回1点
  • CTRL右键单击=取回5分

JavaScript的:

var Alexander =
    {
      Magic: "MagicVal",
      Attack: "AttackVal",
      Bonus: "BonusVal",
      Limits: {
        Magic:  {
          max: 100,
          min: 80
        },
        Attack: {
          max: 100,
          min: 80
        }
        }
    };

function table(e)
{
  e.preventDefault();
}

function add(character, stat) //Allocates "1" with left-click//
{
  var txtNumber = document.getElementById(character[stat]);
  var newNumber = parseInt(txtNumber.value) + 1;
  if(newNumber > character.Limits[stat].max) return;
  var BonusVal = document.getElementById(character.Bonus);
  if(BonusVal.value <= 0) return;
  var newBonus = parseInt(BonusVal.value) - 1;
  BonusVal.value = newBonus; 
  txtNumber.value = newNumber;
}

function subtract(character, stat) //Takes back "1" with right-click//
{
  var txtNumber = document.getElementById(character[stat]);
  var newNumber = parseInt(txtNumber.value) - 1;
  if(newNumber < character.Limits[stat].min) return;
  var BonusVal = document.getElementById(character.Bonus);
  var newBonus = parseInt(BonusVal.value) + 1;
  BonusVal.value = newBonus; 
  txtNumber.value = newNumber;
}

function dump(e, character, stat) //Allocates "5" with CTRL+left-click//
{
  if (e.ctrlKey == 1 && e.which == 1) {
  console.log('met');
  var txtNumber = document.getElementById(character[stat]);
  var newNumber = parseInt(txtNumber.value) + 4;
  if(newNumber > character.Limits[stat].max) return;
  var BonusVal = document.getElementById(character.Bonus);
  if(BonusVal.value <= 0) return;
  var newBonus = parseInt(BonusVal.value) - 4;
  BonusVal.value = newBonus; 
  txtNumber.value = newNumber;
}
}

function reclaim(e, character, stat) //Takes back "5" with CTRL+right-click//
{
  if (e.ctrlKey == 1 && e.which == 3) {
  console.log('met');
  var txtNumber = document.getElementById(character[stat]);
  var newNumber = parseInt(txtNumber.value) - 4;
  if(newNumber < character.Limits[stat].min) return;
    var BonusVal = document.getElementById(character.Bonus);
  var newBonus = parseInt(BonusVal.value) + 4;
  BonusVal.value = newBonus; 
  txtNumber.value = newNumber;
}
}

3 个答案:

答案 0 :(得分:3)

在执行任何调整之前,您似乎正在检查Bonus的现有值。因此,如果奖金为1并且您试图从中删除5,则检查将显示值为1并继续数学以将值更改为-4。

我的猜测是你想要应用数学,然后在之后检查0并且如果值&lt;则调整为0。 0

答案 1 :(得分:3)

你必须替换(在你的dump方法中)

  if(BonusVal.value <= 0) return;
  var newBonus = parseInt(BonusVal.value) - 4;

用这个:

  var newBonus = parseInt(BonusVal.value) - 4;
  if(newBonus <= 0) return;

答案 2 :(得分:2)

工作示例!...

http://jsbin.com/omigOti/1/edit

你需要验证奖金是否为负数......就像这次更正......

function add(character, stat) //Allocates "1" with left-click//
{
  var txtNumber = document.getElementById(character[stat]);
  var newNumber = parseInt(txtNumber.value) + 1;
  if(newNumber > character.Limits[stat].max) return;
  var BonusVal = document.getElementById(character.Bonus);
  if(BonusVal.value <= 0) return;
  var newBonus = parseInt(BonusVal.value) - 1;
  if( newBonus < 0 ){ //try if is negative

    newBonus = 0; //and fix it
    newNumber = +(BonusVal.value);

  } 
  BonusVal.value = newBonus; 
  txtNumber.value = newNumber;
}

function dump(e, character, stat) //Allocates "5" with CTRL+right-click//
{
  if (e.ctrlKey == 1 && e.which == 1) {
    debugger;
    console.log('met');
    var txtNumber = document.getElementById(character[stat]);
    var newNumber = parseInt(txtNumber.value) + 4;
    if(newNumber > character.Limits[stat].max) return;
    var BonusVal = document.getElementById(character.Bonus);
    if(BonusVal.value <= 0) return;
    var newBonus = parseInt(BonusVal.value) - 4;
    if( newBonus < 0 ){ //try if is negative

      newBonus = 0; //and fix it
      newNumber = +(BonusVal.value);

    } 
    BonusVal.value = newBonus; 
    txtNumber.value = newNumber;
  }
}