Here’s the code.
I’m using a function called
generateRandTile() to generate a random tile for computer to play, this function returns the id of the generated tile/cell in a function called
pcPlay(), so that the computer knows where to place its next tile.
Now, what happens is that the function
generateRandTile() sometimes returns an
undefined value (with no obvious reason), and this results in the computer not playing its turn.
Edit: the code linked above is not working for some reason. And the problem got fixed, here’s the working one: https://jsbin.com/cumirocute/edit?html,js,output .
In the ‘else’ branch of your generateRandTile() function, where you generate a tile again, you need to “return generateRandTile();”.
You also need to be careful to not call generateRandTile more than 9 times or it’ll get caught in an endless loop.
Oh I see, so the way I was doing it before is that I was returning the result in
generateRandTile() not in
Thanks for the tip. I’m having many
too much recursion errors on the last front end projects of freecodecamp.
I don’t know about pcPlay(), I only looked at generateRandTile() - I saw that it was being called recursively and that the result wasn’t being sent back up the callstack, hence the need for the “return” in the ‘else’ branch.
How does it work now with the correction in place?
It stopped working after the correction, when you first click to show a stone (x/o), it throws off a
showStoneOnClick is not defined error.
Are you working in CodePen?
No, I’m writing code locally.
Did you include your js file at the top of the html file?
I don’t think I’ll be able to help much more without actually seeing your code running.
The code is included in the first post.
Jsfiddle is just like codepen, it’s a js play ground.
Here’s the link again: https://jsfiddle.net/z2s49znm/ .
Edit: It seems to have stopped working for another reason, I’ll check it out and report back to you.
It seems to be working fine now. I got confused because I was trying multiple js play grounds, and one of them (jsfiddle) seems not to be working for some reason.
Here’s the working one: https://jsbin.com/cumirocute/edit?html,js,output .