This function is supposed to produce an array of random points on an axis and keep the last point in the array as a final ‘choice’. The function is called repeatedly by user action and the function is supposed to ensure that any final choice point is not repeated in future function calls.
For an unknown reason the equality logic is not working as expected. I tried ‘.includes()’ originally to check the pastChoices array and it didn’t catch repetitions. I created the console.log series shown below to verify that the equality operator is not working in this context.
1 import random from "lodash/random.js";
2
3 let pastChoices = [];
4
5 export function compAttack() {
6 let choiceArray = [];
7 let choice;
8 console.log(pastChoices);
9 for (let i = 0; i <= 25; i++) {
10 let y = random(1, 10);
11 let x = random(1, 10);
12 choiceArray.push(`${y},${x}`);
13 if (i != 25) continue;
14 choice = choiceArray[choiceArray.length - 1].split(",");
15 pastChoices.forEach((x) => {
16 console.log(x);
17 console.log(choice);
18 console.log(choice == x);
19 });
20 }
21 pastChoices.push(choice);
22 }
Console Output:
02:23:25.786 compAttack.js:16 (2) ['7', '2']
02:23:25.787 compAttack.js:17 (2) ['9', '10']
02:23:25.788 compAttack.js:18 false
02:23:25.788 compAttack.js:16 (2) ['9', '10']
02:23:25.789 compAttack.js:17 (2) ['9', '10']
02:23:25.789 compAttack.js:18 false
02:23:25.789 compAttack.js:16 (2) ['6', '2']
02:23:25.789 compAttack.js:17 (2) ['9', '10']
02:23:25.789 compAttack.js:18 false
I’ve been able to come up with another way to achieve my end goal (not using equality) but I’m curious as to why this is happening?
Is it a runtime issue? Or more likely, is it a me issue?
I pulled the function out of my application (refactored it a little) and ran it in a stand alone file to see if the behavior would be consistent and lo and behold.
I’m expecting “repeat” to print to the console but it doesn’t.
import random from "lodash/random.js";
let pastChoices = [];
export function compAttack() {
let choiceArray = [];
let choice;
console.log(pastChoices);
for (let i = 0; i <= 25; i++) {
let y = random(1, 10);
let x = random(1, 10);
choiceArray.push(`${y},${x}`);
if (i != 25) continue;
choice = choiceArray[choiceArray.length - 1].split(",");
if (pastChoices.includes(choice)) {
console.log("repeat");
}
}
pastChoices.push(choice);
}
for (let i = 0; i < 50; i++) {
compAttack();
}
Console Output – This is the finally array after the last call and there are repetitions but ‘repeat’ never printed to console at any point.
[
[ '3', '5' ], [ '8', '2' ], =>[ '4', '2' ],
[ '8', '9' ], [ '9', '6' ], [ '10', '10' ],
[ '9', '9' ], [ '2', '4' ], [ '4', '5' ],
[ '6', '9' ], [ '4', '10' ], [ '7', '6' ],
=>[ '4', '2' ],[ '10', '4' ], [ '8', '5' ],
[ '5', '1' ], [ '9', '1' ], [ '5', '3' ],
[ '4', '8' ], [ '5', '5' ], [ '3', '8' ],
[ '8', '2' ], [ '7', '9' ], [ '6', '7' ],
[ '1', '1' ], [ '7', '9' ], [ '5', '6' ],
[ '6', '4' ], [ '8', '1' ], [ '6', '3' ],
[ '1', '3' ], [ '8', '8' ], [ '8', '4' ],
[ '1', '5' ], [ '3', '2' ], [ '6', '3' ],
[ '2', '10' ], [ '5', '7' ], [ '2', '1' ],
[ '7', '1' ], [ '1', '9' ], [ '5', '8' ],
[ '5', '2' ], [ '10', '6' ], [ '2', '6' ],
[ '6', '2' ], [ '1', '2' ], [ '4', '2' ],
[ '8', '5' ]
]