gpt4 book ai didi

javascript - JS Loop 不会循环,直到我注释掉我的 switch 方法

转载 作者:行者123 更新时间:2023-11-28 19:03:01 24 4
gpt4 key购买 nike

被一个问题困扰了几个小时,我的 for 循环无法循环(仅进行 1 次迭代)。

以下是我的代码:

function getScore(summonerId) {

$.ajax({
dataType: "json",
type: 'GET',
url: 'https://na.api.pvp.net/api/lol/na/v2.2/matchhistory/' + summonerId + '/?queueType=NORMAL_5x5_BLIND,NORMAL_5x5_DRAFT,RANKED_SOLO_5x5,RANKED_PREMADE_5x5,NORMAL_5x5_DRAFT,RANKED_TEAM_5x5&beginIndex=0&endIndex=9&api_key=***',
success: function (data, state) {

for (i = 0; i < 10; i++){ // THIS IS THE LOOP THAT WON'T WORK.
console.log(i);
console.log(state);
//v1.1.0

if (data.matches[i].participants[0].stats.firstBloodKill == true){
firstBloodArray[i] = 1;
}else{
firstBloodArray[i] = 0;
}

if (data.matches[i].participants[0].stats.firstTowerKill == true){
firstTowerArray[i] = 1;
}else{
firstTowerArray[i] = 0;
}

if (data.matches[i].participants[0].stats.firstTowerKill == true){
firstInhibitorArray[i] = 1;
}else{
firstInhibitorArray[i] = 0;
}

damageDealtArray[i] = data.matches[i].participants[0].stats.totalDamageDealt;
healingDealtArray[i] = data.matches[i].participants[0].stats.totalHeal;
damageTakenArray[i] = data.matches[i].participants[0].stats.totalDamageTaken;
inhibitorArray[i] = data.matches[i].participants[0].stats.inhibitorKills;
towerArray[i] = data.matches[i].participants[0].stats.towerKills;

//timeline arrays

tl_arr_cspm_zero_ten[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.zeroToTen;
tl_arr_cspm_ten_twenty[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.tenToTwenty;

if(data.matches[i].participants[0].timeline.creepsPerMinDeltas.twentyToThirty != undefined) {
tl_arr_cspm_twenty_thirty[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.twentyToThirty;
}else{
tl_arr_cspm_twenty_thirty[i] = 0
}

if(data.matches[i].participants[0].timeline.creepsPerMinDeltas.thirtyToEnd != undefined) {
tl_arr_cspm_thirty_end[i] = data.matches[i].participants[0].timeline.creepsPerMinDeltas.thirtyToEnd;
}else{
tl_arr_cspm_thirty_end[i] = 0
}

tl_arr_xppm_zero_ten[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.zeroToTen;
tl_arr_xppm_ten_twenty[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.tenToTwenty;

if(data.matches[i].participants[0].timeline.xpPerMinDeltas.twentyToThirty != undefined) {
tl_arr_xppm_twenty_thirty[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.twentyToThirty;
}else{
tl_arr_xppm_twenty_thirty[i] = 0;
}

if(data.matches[i].participants[0].timeline.xpPerMinDeltas.thirtyToEnd != undefined){
tl_arr_xppm_thirty_end[i] = data.matches[i].participants[0].timeline.xpPerMinDeltas.thirtyToEnd;
}else{
tl_arr_xppm_thirty_end[i] = 0;
}


tl_arr_gpm_zero_ten[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.zeroToTen;
tl_arr_gpm_ten_twenty[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.tenToTwenty;

if(data.matches[i].participants[0].timeline.goldPerMinDeltas.twentyToThirty != undefined){
tl_arr_gpm_twenty_thirty[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.twentyToThirty;
}else{
tl_arr_gpm_twenty_thirty[i] = 0;
}

if(data.matches[i].participants[0].timeline.goldPerMinDeltas.thirtyToEnd != undefined){
tl_arr_gpm_thirty_end[i] = data.matches[i].participants[0].timeline.goldPerMinDeltas.thirtyToEnd;
}else{
tl_arr_gpm_thirty_end[i] = 0;
}


//v1.0.2

csArray[i] = data.matches[i].participants[0].stats.minionsKilled;
assistArray[i] = data.matches[i].participants[0].stats.assists;
killArray[i] = data.matches[i].participants[0].stats.kills;
gameTimeArray[i] = data.matches[i].matchDuration;
championArray[i] = data.matches[i].participants[0].championId;
deathArray[i] = data.matches[i].participants[0].stats.deaths;
jungleCSArray[i] = data.matches[i].participants[0].stats.neutralMinionsKilledTeamJungle;
counterJungleCSArray[i] = data.matches[i].participants[0].stats.neutralMinionsKilledEnemyJungle;
roleArray[i] = data.matches[i].participants[0].timeline.role;
laneArray[i] = data.matches[i].participants[0].timeline.lane;
crowdControlArray[i] = data.matches[i].participants[0].stats.totalTimeCrowdControlDealt;

switch (roleArray[i]) {
case "DUO":
case "SOLO":
//assumes top lane duo player or solo mid or solo top
calculateHmc(1.33, 1, 0.5, 0.06, 0.11, 0.17, 5, 0.5, 4, 0.5, 0.16, 0.65, 2, 1.25, 3, 1.5, 1, 0.9, 0.8, 0.7, 1, 1.1, 1.1, 1.1, 1, 1.1, 1.2, 1.3, i);
break;
case "DUO_CARRY":
//assumes bot lane adc
calculateHmc(1.33, 1, 0.5, 0.06, 0.11, 0.17, 5, 0.5, 4, 0.5, 0.16, 0.65, 2, 1.25, 3, 2, 1, 0.9, 0.8, 0.7, 0.8, 0.8, 0.8, 0.8, 1, 1.1, 1.2, 1.3, i);
break;
case "DUO_SUPPORT":
//assumes bot lane support
calculateHmc(1.33, 1, 0.75, 0.06, 0.11, 0.17, 0.5, 5, 4, 0.5, 0.16, 0.65, 2, 1.25, 0.25, 3.5, 0.1, 0.1, 0.1, 0.1, 0.8, 0.8, 0.8, 0.8, 0.3, 0.4, 0.5, 0.6, i);
break;
case "NONE":
//assumes jungle, check lane
calculateHmc(1.33, 1, 0.5, 0.06, 0.11, 0.17, 2, 2.5, 4, 0.5, 0.16, 0.65, 2, 1.25, 1.5, 1.5, 0.6, 0.5, 0.4, 0.3, 1, 0.9, 0.8, 0.7, 1, 0.9, 0.8, 0.7, i);
break;
}
}


}
});

如果我注释掉开关,循环就会起作用。

我尝试重写循环以确保循环声明中没有语法错误,但事实并非如此。

我需要循环10次并调用calculateHmc函数10次,但它只执行一次。

一旦 switch (roleArray[i]) 被注释掉,循环就会工作,但显然它不会调用该函数。

有什么想法为什么会发生这种情况吗?

最佳答案

我大胆猜测您在 for 语句中使用的全局变量 i 会被 calculateHMC() 击败> 导致 for 循环过早终止。

无论如何,更改 for 语句以使用局部变量而不是隐式全局变量:

for (var i = 0; i < 10; i++)

并且,检查 calculateHMC() 是否存在类似问题。即使这不是代码中的唯一问题,它也是一个应该修复的问题。

<小时/>

事实上,如果您在 strict mode 中运行代码,它会向您指出所有这些错误,当然,这也是使用严格模式的原因之一,这样您就不会意外地犯下此类错误。

关于javascript - JS Loop 不会循环,直到我注释掉我的 switch 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32194351/

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