Need help with TicTacToe project computer AI functionality

Hi campers,

I am working on the TicTacToe project. I have been stuck for about a week trying to get my computerTurn function to work properly. In the 2 player game option, everything works great, however when you choose 1 player game, and make the first move (which shows up fine), the computer’s move doesnt show up. I have used a nested array called “grid” to link to the html to show the moves. I can see when i console.log the grid that the computer is picking a spot, I just can’t figure out how to target that spot properly and dislplay the “X” or “O” for the computer. Also, I noticed from the console.log(grid) that when I hit the “play again” button instead of refresh the page, the computer moves start the get buggy and not work properly the second time around.

My goal is to find a solution without changing the entire structure of my code. I have looked at other coder’s projects and can’t seem to make their logic work with my code structure. I am still fairly inexperienced, and I know there is probably something small I am missing, just can’t seem to put my finger on it.

Any help or advice would be greatly appreciated! Here is a link to my codepen:

Thanks in advance!

I believe you need some sort of timing function like setTimeout().

Hi Randell,

Thanks for taking the time to look over my mess of a code and offer some solid advice. Like I said I am inexperienced with 0 schooling except for what I’ve learned on FCC. I will definitely take your advice and start over, trying to plan better before I start to write the code. Also thanks for bringing the eq function to my attention, I was not aware it existed. I will have to spend more time researching jQuery functions.

Cheers!