It is necessary that the computer after the person’s move (the call to the click1 function) generates a random number from 1 to 9, sets it to “s” and checks if it is empty, put “Y”, if busy, re-generate the number ... some problem in the code I can not understand:

var s1 = document.getElementById("s1") var s2 = document.getElementById("s2") var s3 = document.getElementById("s3") var s4 = document.getElementById("s4") var s5 = document.getElementById("s5") var s6 = document.getElementById("s6") var s7 = document.getElementById("s7") var s8 = document.getElementById("s8") var s9 = document.getElementById("s9") var comChoice=0; var clickScore=0; function click1(element){ clickScore++ comChoice = Math.floor((Math.random() * 9)+1); console.log(comChoice) if(element.innerText==""){ if(clickScore%2==1 ){ element.style.cursor= "default" element.style.color="white" element.style.fontSize="40px" element.style.textAlign="center" element.innerHTML="X" } else{ element.style.cursor= "default" element.style.color="white" element.style.fontSize="40px" element.style.textAlign="center" element.innerHTML="O" } } else { alert("Ячейка уже занята") } computerTurn() if (proverka()==true ){ alert("end") } } function computerTurn(){ comChoice = Math.floor((Math.random() * 9)+1); if (s[comChoice].innerHTML=="X"||s[comChoice].innerHTML=="Y"){ computerTurn() } else { s[comChoice].innerHTML=="Y" } } 
  • I advise reading the information about arrays - Grundy
  • @Lucky_girl, a bad idea to change the code in question, in which you need to find out why the code is not working - Grundy
  • @Grundy, but I did not change the code in question, I corrected spelling and punctuation. - Lucky_girl
  • @Lucky_girl, clickScore++ was moved to the beginning of the function - Grundy
  • one
    @IvanPshenitsyn, I believe that the code should be monotonous, and since you are putting ; put it everywhere - Grundy

1 answer 1

The problem is the absence of the variable s . And since it is not, it can not be accessed

 s[comChoice] 

Therefore it is worthwhile to start it, for example,

 var s = [s1,s2,s3,s4,s5,s6,s7,s8,s9]; 

It should be borne in mind that in this case, the indexation will start from 0, therefore in the calculation of the cell you do not need to add 1

 comChoice = Math.floor(Math.random() * 9); 
  • in fact, during this time I did it (I did the array, but in the matrandome I deleted “+1”), I don’t quite understand one moment how the function can call itself, for example if it checks if there is a character in element, say if it is already taken, how to generate the number again and call this function again (it will turn out to call itself) - Tigran
  • one
    @Tigran, Recursion , Well, specifically, it was possible to get by with the cycle: while busy - get the coordinate - Grundy
  • Now I will try, and not on this topic, but it’s still not clear why in if (proverka () == true) if I write without "true", that is, if (proverka ()) (it’s basically the same thing) doesn't work like that? - Tigran
  • @Tigran, I don’t know why it cann’t work, the records are equivalent - Grundy
  • I thank, probably doing something wrong - Tigran