Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBrett Carter
Full Stack JavaScript Techdegree Graduate 17,766 PointsTrouble incrementing object property - TicTacToe Project
Hello! I have a constructor function 'Game' which constructs a new game with a property "turnIndex" set to 0. The prototype function "startGame" of Game is supposed to set 'this.turnIndex' to 1, yet it is not working. Also, Game.proptotype.nextTurn() is supposed to increment turnIndex by one, which is not working either.
I tried declaring the new game (game1) as a global variable thinking scope may be an issue, but this was unsuccessful.
Below is the code and thank you in advance for the help!
let startHTML = '<div class="screen screen-start" id="start"><header><h1>Tic Tac Toe</h1><a href="#" class="button">Start game</a></header></div>';
let startScreen;
let startButton;
let player1;
let player2;
let game1;
let body = document.getElementsByTagName('body')[0];
/*------------------- DOMContentLoaded Listener -------------------*/
document.addEventListener('DOMContentLoaded', () => {
body.innerHTML += startHTML;
startScreen = document.getElementsByClassName('screen-start')[0];
startButton = document.getElementsByClassName('button')[0];
//event listener which creates the players and hides the start screen
startButton.addEventListener('click',()=>{
player1 = new Player (prompt('PLAYER ONE: Enter your name.'));
player2 = new Player (prompt('PLAYER Two: Enter your name.'));
player1.card = document.getElementById('player1');
player2.card = document.getElementById('player2');
$(startScreen).hide();
game1 = new Game (player1, player2);
game1.startGame();
});
});
/*------------------- Constructor Functions -------------------*/
function Player (name){
this.name = name;
this.score = 0;
this.card;
};
function Game (player1, player2){
this.player1 = player1;
this.player2 = player2;
this.turnIndex = 0;
};
/*------------------- Prototype Functions -------------------*/
Game.prototype.nextTurn = () => {
$(player1.card).toggleClass('active');
$(player2.card).toggleClass('active');
this.turnIndex += 1;
};
Game.prototype.startGame = () => {
this.turnIndex = 1;
if(Math.floor(Math.random()*2 <=1)){
console.log('starting game');
$(player1.card).addClass('active');
} else {
$(player2.card).addClass('active');
}
};
2 Answers
Damien Watson
27,419 PointsHey, I believe you issue is in the prototype functions.
The arrow function expression does not have it's own 'this' and shouldn't be used for method functions.
Changing your code to this should fix your index issue:
Game.prototype.nextTurn = function() {
...
}
Game.prototype.startGame = function() {
...
}
Brett Carter
Full Stack JavaScript Techdegree Graduate 17,766 PointsChanging the prototype functions to a regular function expression did the trick. Thank you!