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 trialTushar Singh
Courses Plus Student 8,692 PointsTic Tac Toe
http://codepen.io/tushar_13/pen/YqjRQv
I have no idea why it is not working, not even displaying icons(function symbDisp).
I have used a function "game" at the end of the js file which should work when a user clicks on the gameboard and has all the functions in it, but there's a problem-- not at all working.
3 Answers
Andrew Trigg
17,485 PointsHi Tushar, I can't give you a complete answer right now, but after a quick look I can see that in your function game()
the functions you call from within that don't know anything about the click event that initiated game(). If you pass the event into the game function like this - game(event)
you can then pass the event as an argument to the other functions, for example the symbDisp(event)
. Then in the actual symbDisp() function you can replace the if($(this).text()==='')
with if(event.target.textContent === '')
to check if there is anything already in the box. I hope this helps a bit.
Andrew Trigg
17,485 PointsTo get your symbols put into the grid, first three lines of your symbDisp():
function symbDisp (e) {
if(e.target.innerText==='') {
$(e.target).text(player);
And first two lines of your game():
function game(e){
symbDisp(e);
Tushar Singh
Courses Plus Student 8,692 PointsI did what you asked me to although I don't understand what I did exactly(we can figure this out later, first get this working) but it is not working, what else can I do?
Tushar Singh
Courses Plus Student 8,692 PointsBut I think you are right, the problem is linking these functions together because everything else seems ok (at least to me)
Andrew Trigg
17,485 PointsChange lines 24 and 25 to this:
if(e.target.innerText==='') {
$(e.target).text(player);
Tushar Singh
Courses Plus Student 8,692 PointsI already did that.
function symbDisp (e) {
if(e.target.innerText()==='') {
$(e.target).text(pTurn);
if(pTurn == player){
pTurn =comp;
}else{
pTurn=player;
}
}
}
Here pTurn = player
var pTurn=player;
And as you asked me to make some changes in game as well-done!
function game(e){
symbDisp(e);
winCheck();
turns+=1;
if(gameEnd=false && turns%2==0){
AI();
DrawIcon(id);
winCheck();
turns+=1;}
}
});
Still not working.
Andrew Trigg
17,485 PointsYou are invoking
e.target.innerText()
as a function - you need to get rid of the parentheses. If you just copy my lines into yours, replacing lines 24 and 25, you will at least be able to put X's or O's into your table.
Tushar Singh
Courses Plus Student 8,692 PointsI got this symbol thing to work
see http://codepen.io/tushar_13/pen/zqPQOp
But it is showing a weird behaviour, what to do about it??
And I really like to thank you for your time, it means a lot. Mostly people just want to answer stupid coding challenges.Thank you. Now let's solve this problem.
Tushar Singh
Courses Plus Student 8,692 PointsOh sorry, I finally understand what you were talking about, that was so dumb of me, Thanks for your patience.
http://codepen.io/tushar_13/pen/zqPQOp
If you check it now, I think the only problem is it toggles the icons again and again, why is that??
Tushar Singh
Courses Plus Student 8,692 PointsTushar Singh
Courses Plus Student 8,692 PointsEvery function is written clearly, I don't think you would face any problem understanding the code. Any help would be appreciated.