0

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;
}
E Gale
  • 1
  • 1
  • `for (let i=0; i<1000; i++) {` ---> is the index variable `i` from this loop being used within the loop? Also: `winners[player.name] += 1 / bestScore.count;`, suppose before this expression `winners` array elt for some `player.name` (say 'John`) is 0, and the `bestScore.count` is 5, then after this statement is the value expected to be: `0.2`? – jsN00b May 29 '22 at 09:46
  • HI There, i added more code. it is supposed to simulate scoring for a player multiple times to see what the average score is – E Gale May 29 '22 at 18:36
  • This is not an error. It's a warning by the linter. If you are not confused by the [semantics of closures in loops](https://stackoverflow.com/q/750486/1048572), turn off that rule. – Bergi May 29 '22 at 21:50
  • Which line are you getting the warning on? – Bergi May 29 '22 at 21:51
  • Could you properly indent the code, please? It's very hard to read. – Bergi May 29 '22 at 21:52
  • What's with this `({a})` suffice in the message? – Bergi May 29 '22 at 21:53
  • I was getting the error on this line - "playersCopy.forEach(player=>{" – E Gale May 29 '22 at 21:53
  • It might go away if you use a normal `for (const player of playersCopy)` loop instead of calling the `forEach` method with a callback – Bergi May 29 '22 at 21:54
  • `return [...Array(holeDist.length).keys()].find(ix => holeDist[ix] > random) - 2;` --> May I please know where the variable `random` in this line of code is declared, and assigned any value? Just one line above, we have: `const randNum = Math.random();` & this `randNum` variable is not used within the scope where it's declared, I think. – jsN00b May 30 '22 at 01:50
  • i added the entire code. It is for golf. I appreciate the assistance – E Gale May 30 '22 at 02:31

0 Answers0