I have the following function that makes absolutely no sense why it fails:
function GetPlayersMap ( map, id ){
let compressedMap = [];
for(let x = 0; x < map.length; x++){
for(let y = 0; y < map[x].length; y++){
if(map[x][y].claimant_id != null) {console.log(map[x][y].claimant_id); console.log(id)}
if(id == null || map[x][y].claimant_id != id){
map[x][y].count = null;
}
if(map[x][y].claimant_id != null){
console.log(map[x][y]);
compressedMap.push(map[x][y]);
}
}
}
return compressedMap;
}
map
is a 2d array of objects, map.count
is an int that is never null
when entering the function. id
is an int that can be null
. The expected result is that on an id
input of 0
it return a compressedMap
with one object that matched that. The function is called twice, with the same map
and an id
of 0
then null
. What is printed in console is
0
0
Tile { x: 0, y: 0, claimant_id: 0, count: null, fake_claimed: false }
0
null
Tile { x: 0, y: 0, claimant_id: 0, count: null, fake_claimed: false }
This is printed regardless of if I change the 5th line to
if(id == null){
(which makes no sense, this means it is matching 0 to null)or
if(map[x][y].claimant_id != id){
Only when I change it to
if(false){
do I get the expected output of
0
0
Tile { x: 0, y: 0, claimant_id: 0, count: 1, fake_claimed: false }
0
null
Tile { x: 0, y: 0, claimant_id: 0, count: 1, fake_claimed: false }
I added a simplified example of the code
class Tile {
constructor(x, y) {
this.x = x;
this.y = y;
this.claimant_id = null;
this.count = 1;
this.fake_claimed = false;
}
}
var map = []
for (let x = 0; x < 1000; x++) {
map.push([]);
for (let y = 0; y < 1000; y++) {
map[x].push(new Tile(x, y));
}
}
map[0][0].claimant_id = 0;
function GetPlayersMap(map, id) {
let compressedMap = [];
for (let x = 0; x < map.length; x++) {
for (let y = 0; y < map[x].length; y++) {
if (map[x][y].claimant_id != null) {
console.log(map[x][y].claimant_id);
console.log(id)
}
if (id == null || map[x][y].claimant_id != id) {
map[x][y].count = null;
}
if (map[x][y].claimant_id != null) {
console.log(map[x][y]);
compressedMap.push(map[x][y]);
}
}
}
return compressedMap;
}
GetPlayersMap(map, 0);
GetPlayersMap(map, null);
GetPlayersMap(map, 0);