I have a loop as follows. That needs to update a score by counting, how many tiles a player has. As per comments, the toy can see, which accessors work and which don't:
Player.prototype.calculateScore = function(){
for(i = 0; i < 9; i ++){
//works and prints tile1 player contents
console.log(game.tile.tile1.player);
//doesnt work
console.log(game.tile['tile' + i]['player']);
//works and prints the entire tile1 object
console.log(game.tile['tile' + i]);
//if(game.tile['tile' + i]['player'] == this.name){
// this.score = this.score + 1;
//}
}
}
here is the object containing the data
function Game(){
this.tile = {
'tile1' : {card: '', player: ''},
'tile2' : {card: '', player: ''},
'tile3' : {card: '', player: ''},
'tile4' : {card: '', player: ''},
'tile5' : {card: '', player: ''},
'tile6' : {card: '', player: ''},
'tile7' : {card: '', player: ''},
'tile8' : {card: '', player: ''},
'tile9' : {card: '', player: ''}
};
Am I trying to access it incorrectly? I am currently running code on a node.js server running with socket.io .