gpt4 book ai didi

javascript - 如何将每次计算的 currentIndex 值设置为 0

转载 作者:行者123 更新时间:2023-12-03 02:02:36 24 4
gpt4 key购买 nike

如何将 currentIndex 的值更改为始终等于 0。

(CRANK1 + CRANK2) + (DRANK1 + DRANK2) 的价格应为 (0 + 选择的金额),但其相加为 (选择的金额 + 选择的金额)

感谢任何帮助,谢谢。 帖子主要是代码需要文本帖子主要是代码需要文本帖子主要是代码需要文本

var current_division,
desired_division;


var prices = [
00,09,09,09,09,
12,12,14,14,14,
17,17,17,17,19,
23,24,25,28,28,
35,40,50,65,85,
120,00,00,00,00
];


function getCurrentIndex() {
return (+document.getElementById("CRANK1").value +
+document.getElementById("CRANK2").value);

}

function getDesiredIndex() {
return (+document.getElementById("DRANK1").value +
+document.getElementById("DRANK2").value);
}


function total() {
var currentIndex = getCurrentIndex();
var desiredIndex = getDesiredIndex();

if (desiredIndex < currentIndex) {
document.getElementById('prices').value = "You can't rank backwards";
return;
}

var accumulatedPrice = 0;
for(var i = currentIndex; i <= desiredIndex; i++) {
accumulatedPrice += prices[i];
}

document.getElementById('prices').value = accumulatedPrice;
document.getElementById("prices").readOnly = true;

}

document.getElementById('divboost').addEventListener('change', function() {
total();
})

HTML

  <form id="divboost" name="priceCalc" action="">
<br/>
<select id="CRANK1"> Current Rank
<option value="0">Bronze</option>
<option value="5">Silver</option>
<option value="10">Gold</option>
<option value="15">Platinum</option>
<option value="20">Diamond</option>
</select>
<br>
<br/>
<select id="CRANK2"> Current Divison
<option value="0">Division 5</option>
<option value="1">Division 4</option>
<option value="2">Division 3</option>
<option value="3">Division 2</option>
<option value="4">Division 1</option>
</select>
<br>
<br>
<br/>
<br/>
<select id="DRANK1"> Desired Rank
<option value="0">Bronze</option>
<option value="5">Silver</option>
<option value="10">Gold</option>
<option value="15">Platinum</option>
<option value="20">Diamond</option>
<option value="25">Master</option>
</select>
<br>
<br/>
<select id="DRANK2"> Desired Divison
<option value="0">Division 5</option>
<option value="1">Division 4</option>
<option value="2">Division 3</option>
<option value="3">Division 2</option>
<option value="4">Division 1</option>
</select>
<br>
<br>
€ <input type="text" id="prices">
<br/>
<br>
</form>

最佳答案

经过一番尝试后,我注意到您不想将第一个 currentIndex 添加到您的 accumulatedPrice 中。

通过在循环之前递增 currentIndex,您应该获得所需的预期行为

  var accumulatedPrice = 0;
currentIndex++;
for(var i = currentIndex; i <= desiredIndex; i++) {
accumulatedPrice += prices[i];
}

或者您可以将 getCurrentIndex() 函数的返回值加 1

关于javascript - 如何将每次计算的 currentIndex 值设置为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49947030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com