JS-在引用外部范围变量的循环中声明的函数可能会导致语义令人困惑。 ({一个})

发布于 2025-02-03 03:23:44 字数 3729 浏览 1 评论 0原文

我正在下面的工作。我对此很新手。

我不确定为什么会遇到此错误 - 在引用外范围变量的循环中声明的功能可能会导致令人困惑的语义。 ({a})

错误即将出现在线-promentercopy.foreach(player => {

我试图在下面将其粗体。我试图将其清除,以便在这里看起来很干净。感谢您的帮助。

enter code here

// Note: the Object keys are equal to the score under/over par.  -2 = eagle, -1 = birdie, 0 = par, 1 = bogey, 2 = double bogey
let holeProbs = {
1: {
  "-2": 0,
  "-1": 0.21,
  "0": 0.67,
  "1": 0.12,
  "2": 0
},
2: {
  "-2": 0,
  "-1": 0.23,
  "0": 0.70,
  "1": 0.07,
  "2": 0
},
3: {
  "-2": 0,
  "-1": 0.21,
  "0": 0.67,
  "1": 0.12,
  "2": 0
},
4: {
  "-2": 0,
  "-1": 0.149,
  "0": 0.759,
  "1": 0.089,
  "2": 0.003
},
5: {
  "-2": 0.01,
  "-1": 0.375,
  "0": 0.425,
  "1": 0.145,
  "2": 0.045
},
6: {
  "-2": 0,
  "-1": 0.05,
  "0": 0.70,
  "1": 0.20,
  "2": 0.05
},
7: {
  "-2": 0,
  "-1": 0.24,
  "0": 0.64,
  "1": 0.07,
  "2": 0.05
},
8: {
  "-2": 0.0075,
  "-1": 0.4575,
  "0": 0.4875,
  "1": 0.0475,
  "2": 0
},
9: {
  "-2": 0,
  "-1": 0.20,
  "0": 0.62,
  "1": 0.14,
  "2": 0.04
},
10: {
  "-2": 0,
  "-1": 0.21,
  "0": 0.69,
  "1": 0.08,
  "2": 0.02
},
11: {
  "-2": 0.01,
  "-1": 0.3275,
  "0": 0.5775,
  "1": 0.0675,
  "2": 0.0175
},
12: {
  "-2": 0,
  "-1": 0.2875,
  "0": 0.6075,
  "1": 0.0975,
  "2": 0.0075
},
13: {
  "-2": 0,
  "-1": 0.10,
  "0": 0.71,
  "1": 0.15,
  "2": 0.04
},
14: {
  "-2": 0,
  "-1": 0.2575,
  "0": 0.6675,
  "1": 0.0575,
  "2": 0.0175
},
15: {
  "-2": 0,
  "-1": 0.0925,
  "0": 0.8025,
  "1": 0.1025,
  "2": 0.0025
},
16: {
  "-2": 0.01,
  "-1": 0.395,
  "0": 0.525,
  "1": 0.065,
  "2": 0.005
},
17: {
  "-2": 0.00,
  "-1": 0.1125,
  "0": 0.7025,
  "1": 0.0825,
  "2": 0.1025
},
18: {
  "-2": 0,
  "-1": 0.1425,
  "0": 0.6825,
  "1": 0.1325,
  "2": 0.0425
} };

const playerAdjustments = {
"Max Homa": 0.279,
"Si Woo Kim": 0.267,
"Tony Finau": 1.243,
"Richy Werenski": 0.484,
"Russell Knox": 0.030,
"Francesco Molinari": 0.038,
"Doug Ghim": -0.520 };

holeProbs = Object.values(holeProbs).map(hole=>{
const sortedKeys = 
Object.keys(hole).sort((a,b)=>parseInt(a)>parseInt(b)?1:-1);
let sum=0;
const sums=[];
for (const key of sortedKeys) {
    sum+=hole[key];
    sums.push(sum);
}
return sums; });

function randomHoleScore(holeDist) {
const randNum = Math.random();
return 
[...Array(holeDist.length).keys()].find(ix=>holeDist[ix]>random)-2; }

function playerRound(playerObj, courseObj) {
Object.values(courseObj).forEach(holeDist=>{
holeDistCopy = JSON.parse(JSON.stringify(holeDist));
holeDistCopy[1] += playerObj.SGT / (18 * 2);
holeDistCopy[2] += playerObj.SGT / (18 * 2);
playerObj.score += randomHoleScore(holeDistCopy); }); }

function simulateRound(playersArray, courseObj) {
playersArray.forEach(player=>playerRound(player, courseObj));
const bestScore = 
Math.max(...playersArray.map(player=>player.score));
const winners = players.filter(player=>player.score=bestScore);
return winners; }

function winProbSimulator(playersArray, courseObj, N=100) {
const winners = {};
playersArray.forEach(player=>winners[player.name]=0);
for (let I=0; I<N; I++) {
const playersCopy = JSON.parse(JSON.stringify(playersArray));
const bestScore = {score: 1000, count: 1};
   ***playersCopy.forEach(player=>{***
        playerRound(player, courseObj);
        if (player.score < bestScore.score) {
            bestScore.score = player.score;
            bestScore.count = 1;
        }
        else if (player.score === bestScore.score) {
            bestScore.count += 1;
        }
    });
    playersCopy.forEach(player=>{
        if (player.score===bestScore.score) {
            winners[player.name] += 1 / bestScore.count;
        }
    });
}
Object.keys(winners).forEach(key=> winners[key] /= N);
return winners;
}

I am working on the below. I am novice with this.

I am not sure why I am getting this error - Functions declared within loops referencing an outer scoped variable may lead to confusing semantics. ({a})

The error is coming at line - playersCopy.forEach(player=>{

I tried to bold it below. I tried to clean it up so it would look clean on here. I appreciate the assistance.

enter code here

// Note: the Object keys are equal to the score under/over par.  -2 = eagle, -1 = birdie, 0 = par, 1 = bogey, 2 = double bogey
let holeProbs = {
1: {
  "-2": 0,
  "-1": 0.21,
  "0": 0.67,
  "1": 0.12,
  "2": 0
},
2: {
  "-2": 0,
  "-1": 0.23,
  "0": 0.70,
  "1": 0.07,
  "2": 0
},
3: {
  "-2": 0,
  "-1": 0.21,
  "0": 0.67,
  "1": 0.12,
  "2": 0
},
4: {
  "-2": 0,
  "-1": 0.149,
  "0": 0.759,
  "1": 0.089,
  "2": 0.003
},
5: {
  "-2": 0.01,
  "-1": 0.375,
  "0": 0.425,
  "1": 0.145,
  "2": 0.045
},
6: {
  "-2": 0,
  "-1": 0.05,
  "0": 0.70,
  "1": 0.20,
  "2": 0.05
},
7: {
  "-2": 0,
  "-1": 0.24,
  "0": 0.64,
  "1": 0.07,
  "2": 0.05
},
8: {
  "-2": 0.0075,
  "-1": 0.4575,
  "0": 0.4875,
  "1": 0.0475,
  "2": 0
},
9: {
  "-2": 0,
  "-1": 0.20,
  "0": 0.62,
  "1": 0.14,
  "2": 0.04
},
10: {
  "-2": 0,
  "-1": 0.21,
  "0": 0.69,
  "1": 0.08,
  "2": 0.02
},
11: {
  "-2": 0.01,
  "-1": 0.3275,
  "0": 0.5775,
  "1": 0.0675,
  "2": 0.0175
},
12: {
  "-2": 0,
  "-1": 0.2875,
  "0": 0.6075,
  "1": 0.0975,
  "2": 0.0075
},
13: {
  "-2": 0,
  "-1": 0.10,
  "0": 0.71,
  "1": 0.15,
  "2": 0.04
},
14: {
  "-2": 0,
  "-1": 0.2575,
  "0": 0.6675,
  "1": 0.0575,
  "2": 0.0175
},
15: {
  "-2": 0,
  "-1": 0.0925,
  "0": 0.8025,
  "1": 0.1025,
  "2": 0.0025
},
16: {
  "-2": 0.01,
  "-1": 0.395,
  "0": 0.525,
  "1": 0.065,
  "2": 0.005
},
17: {
  "-2": 0.00,
  "-1": 0.1125,
  "0": 0.7025,
  "1": 0.0825,
  "2": 0.1025
},
18: {
  "-2": 0,
  "-1": 0.1425,
  "0": 0.6825,
  "1": 0.1325,
  "2": 0.0425
} };

const playerAdjustments = {
"Max Homa": 0.279,
"Si Woo Kim": 0.267,
"Tony Finau": 1.243,
"Richy Werenski": 0.484,
"Russell Knox": 0.030,
"Francesco Molinari": 0.038,
"Doug Ghim": -0.520 };

holeProbs = Object.values(holeProbs).map(hole=>{
const sortedKeys = 
Object.keys(hole).sort((a,b)=>parseInt(a)>parseInt(b)?1:-1);
let sum=0;
const sums=[];
for (const key of sortedKeys) {
    sum+=hole[key];
    sums.push(sum);
}
return sums; });

function randomHoleScore(holeDist) {
const randNum = Math.random();
return 
[...Array(holeDist.length).keys()].find(ix=>holeDist[ix]>random)-2; }

function playerRound(playerObj, courseObj) {
Object.values(courseObj).forEach(holeDist=>{
holeDistCopy = JSON.parse(JSON.stringify(holeDist));
holeDistCopy[1] += playerObj.SGT / (18 * 2);
holeDistCopy[2] += playerObj.SGT / (18 * 2);
playerObj.score += randomHoleScore(holeDistCopy); }); }

function simulateRound(playersArray, courseObj) {
playersArray.forEach(player=>playerRound(player, courseObj));
const bestScore = 
Math.max(...playersArray.map(player=>player.score));
const winners = players.filter(player=>player.score=bestScore);
return winners; }

function winProbSimulator(playersArray, courseObj, N=100) {
const winners = {};
playersArray.forEach(player=>winners[player.name]=0);
for (let I=0; I<N; I++) {
const playersCopy = JSON.parse(JSON.stringify(playersArray));
const bestScore = {score: 1000, count: 1};
   ***playersCopy.forEach(player=>{***
        playerRound(player, courseObj);
        if (player.score < bestScore.score) {
            bestScore.score = player.score;
            bestScore.count = 1;
        }
        else if (player.score === bestScore.score) {
            bestScore.count += 1;
        }
    });
    playersCopy.forEach(player=>{
        if (player.score===bestScore.score) {
            winners[player.name] += 1 / bestScore.count;
        }
    });
}
Object.keys(winners).forEach(key=> winners[key] /= N);
return winners;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文