let O1 = false
let X1 = false
let turn = 0
function Xon1() {
if(O1 === false && X1 === false && turn === 0) {
document.getElementById("1").innerHTML = 'X'
let X1 = true
setTimeout(compMove, 1000)}}
function compMove() {
if(X1 === true) {
document.getElementById("5").innerHTML = 'O'}}
td {
border: 1px solid black;
height: 50px;
width: 50px;}
<html>
<body>
</body>
<table>
<tr>
<td align= 'center'><button id="1" onclick="Xon1()">-</button></td>
<td align= 'center'><button>-</button></td>
<td align= 'center'><button>-</button></td>
</tr>
<tr>
<td align= 'center'><button>-</button></td>
<td align= 'center'><button>-</button></td>
<td align= 'center'><button>-</button></td>
</tr>
<tr>
<td align= 'center'><button">-</button></td>
<td align= 'center'><button">-</button></td>
<td align= 'center'><button">-</button></td>
</tr>
</table>
</html>
I keep trying different things but whenever I add a if statement to compMove() it doesn’t run even X1 does equal true. I can not figure our what is wrong please help.
2
Answers
Don’t declare
X1
again inXon1
using let and add IDs to all the boxeshere is the potential code that you are looking for
All the best for the next steps.
First, you’re re-instantiating X1 inside the
if(O1 === false && X1 === false && turn === 0)
statement. That means there are actually TWOX1
variables. The keywordlet
instantiates a variable with very limited scope, which lives within any block of code (in this case, theif
statement).Once a variable is instantiated, you can just update the value with
=
, you don’t needlet
,var
, etc.Second, you have a few syntax errors in your HTML. In the last row of squares, you have
<button">
. Remove the"
in each of those.Third, you don’t have
id
s on any other of the buttons other than the first one. Addid
s to each.And finally, you’re closing the
body
tag before the table. Close thebody
tag right before you close thehtml
tag. Scripts should (generally speaking) go intohead
tags, and it looks like that is what you tried.I edited your post to make it a snippet, btw, which is code that can be run by people who want to help you.
Full example: