首页 > 解决方案 > Custion javascript计算增加10级而不是增加1?

问题描述

所以我有这个自定义代码,我正在作为一个宠物项目工作,我一直在对其进行错误测试,我认为我终于可以用其他选项克隆它,但显然不是?因此,当您在当前 xp 中提供足够的 xp 以滚动到 2 级时,例如,1 级需要 180 XP 才能滚动到 2 级,然后需要 360 才能滚动到 3 级,但是......如果我输入当前现有的xp 是 180,新的 xp 增益为 0,它会跳到 11 级。我已经包含了一张图片来显示这个错误,它也适用于 2 级,我会假设所有级别。谁能帮我解决这个奇怪的错误?这是我第一次尝试在 javascript 中编写代码,所以我很感谢作为语言新手的任何帮助!

编辑:事实证明,测试代码本身也不会产生正确的结果,我确实修复了提到的错字,在正常情况下它仍然没有产生正确的结果,我也不知道为什么。

图片示例 https://i.imgur.com/gGNkPQY.png

javascript本身

var xpToLevel = [180, 360, 540, 720, 900, 1080, 1260, 1440, 1620, 1800, 1980, 2160, 2340, 2520, 2700, 2880, 3060, 3240, 3420, 3600, 3780, 3960, 4140, 4320, 4500, 4680, 4860, 5040, 5220, 5400, 5580, 5760, 5940, 6120, 6300, 6480, 6660, 6840, 7020, 7200, 7380, 7560, 7740, 7920, 8100, 8280, 8460, 8640, 8820, 9000, 9180, 9360, 9540, 9720, 9900, 10080, 10260, 10440, 10620, 10800, 10980, 11160, 11340, 11520, 11700, 11880, 12060, 12240, 12420, 12600, 12780, 12960, 13140, 13320, 13500, 13680, 13860, 14040, 14220, 14400, 14580, 14760, 14940, 15120, 15300, 15480, 15660, 15840, 16020, 16200, 16380, 16560, 16740, 16920, 17100, 17280, 17460, 17640, 17820
],
xpToNext,
newXp,
currentLevel,
currentXp,
currentSpareXp,
goalLevel,
levelBaseXp,
nextLevelXp,
totalXp, 
spareXp,
xpToGoal,
trainerBattle,
officialWin,
officialLoss,
wildPokemon = '<p>Enter your current level, Current XP, and new XP to add in. The new level and new remaining XP will be displayed.</p><p><b>Your Level:</b> <input class="forminput" type="number" min="1" max="100" onKeyUp="inputFullLevel(this, this.value)" id="currentLevel"><br> <b>Current XP:</b> <input class="forminput" type="number" min="0" max="17820" onKeyUp="inputSpareXp(this, this.value)" id="currentXp"><br><b>New XP:</b> <input class="forminput" type="number" min="1" max="17820" onKeyUp="inputXp(this, this.value)" id="newXp"></p><button class="forminput" type="button" onclick="runWildPokemon()">Calculate</button>';


   function switchCalc() {
      var c = document.getElementById("calc_select"),
        calcActive = c.options[c.selectedIndex].value;
      document.getElementById("calc-desc").innerHTML = eval(calcActive);
      document.getElementById("calc-results").innerHTML = '';
            }
    
  function runWildPokemon(){
    currentLevel = document.getElementById("currentLevel").value;
    currentXp = document.getElementById("currentXp").value;
    newXp = document.getElementById("newXp").value;
    const cb = document.getElementById('xpShare');
    const cb2 = document.getElementById('luckyEgg');
    console.log(cb.checked);
    xpToNext = xpToLevel[currentLevel - 1];
    newTotalXp = parseInt(newXp) + parseInt(currentXp);
    newTotalXp *= 0.5;
    
    if (document.getElementById("xpShare").checked == true) {
      newTotalXp *= 0.5;
    }
   
    if (document.getElementById("luckyEgg").checked == true) {
      newTotalXp *= 1.5;
    }
    
    newTotalXp = Math.trunc(newTotalXp);
    
    while(currentXp >= xpToNext) {
      currentXp = currentXp - xpToNext;
      currentLevel = currentLevel + 1;
      xpToNext = xpToLevel[currentLevel - 1];
    }
    document.getElementById("calc-results").innerHTML = '<b>Level:</b> ' + currentLevel +'   <b>XP:</b> ' + '[' + newTotalXp + '/' + xpToNext + ']';
  }

        function inputXp(field, input) {
    if (input > 17820) {
    field.value = 17820;
    }
            if (input < 0) {
    field.value = 0;
        }
    }
        function inputSpareXp(field, input) {
        if (input > 17819) {
    field.value = 17819;
    }
            if (input < 0) {
    field.value = 0;
        }
    }
        function inputFullLevel(field, input) {
        if (input > 100) {
    field.value = 100;
    }
            if (input <= 0) {
    field.value = 1;
        }
    }
        function inputLevel(field, input) {
        if (input > 99) {
    field.value = 99;
    }
            if (input <= 0) {
    field.value = 1;
        }
    }

的HTML

<div id="calculator">
<span id="calcActive" class="wildPokemon"> <b>Select Encounter Type:</b><br> <select class="forminput" id="calc_select" onChange="switchCalc()" style="margin-bottom: 10px;text-align:center;">
  
          <option selected="selected" value="wildPokemon">Wild Pokemon</option>
          <option value="trainerBattle">Trainer/Social</option>
          <option value="officialWin">Official Battle Win</option>
          <option value="officialLoss">Official Battle Loss</option>
        </select>
<input type="checkbox" id="xpShare" name="xpShare" value="xpShare">
<label for="xpShare"><b>XP Share</b></label>
<input type="checkbox" id="luckyEgg" name="luckyEgg" value="luckyEgg">
<label for="luckyEgg"><b>Lucky Egg</b></label><br>
        </span><br>
 <div id="calc-desc">
<p>Enter your current level, Current XP, and new XP to add in. The new level and new remaining XP will be displayed.</p><p><b>Your Level:</b> <input class="forminput" type="number" min="1" max="100" onKeyUp="inputFullLevel(this, this.value)" id="currentLevel"><br> <b>Current XP:</b> <input class="forminput" type="number" min="0" max="17820" onKeyUp="inputSpareXp(this, this.value)" id="currentXp"><br><b>New XP:</b> <input class="forminput" type="number" min="1" max="17820" onKeyUp="inputXp(this, this.value)" id="newXp"></p><button class="forminput" type="button" onclick="runWildPokemon()">Calculate</button>

  </div><p>
  
  </p>
 
 <div id="calc-results">

 </div>
</div>

CSS

#calculator{
width: 300px;
background: var(--back1);
margin: auto;
padding: 10px;
border: 10px solid var(--outline);
}
#calc-desc {font-family: 'Dosis', sans-serif;}

#calc-results b{
font-family: 'Dosis', sans-serif;
font-size: 25px;
}
#calc-results {
display:flex;
justify-content: space-evenly;
font-size: 25px;}

标签: javascripthtml

解决方案


简直是笔误!

您搜索具有 id 的元素currentLevel

currentLevel = document.getElementById("currentLevel").value;

但是在你的 HTML 中它被命名为currentlevel

<input class="forminput" type="number" min="1" max="100" onKeyUp="inputFullLevel(this, this.value)" id="currentlevel">

JavaScript 区分大小写!因此,您永远不会获得当前级别的计算值。


推荐阅读