0
The idea is to sort the letters to write an HTML5 element in this case the "tfoot". But when I change the second letter t by the does not change me right. I know why. is because the index I picked up corresponds to the first element with that innerHTML, in this case the index of t will be the first t which is wrong. Here is the javascript mycode.
word = gameMechanics[1];
let lettertoSwitch = 0; // To be in the scope of the function
let letterToBeSwitchedWith = 0;
let indexLettertoSwitch = 0;
letters.forEach((letter) => {
letter.addEventListener("click", (e) => {
if(turn % 2 === 0){ // If its the first time the user clicks on the letter
lettertoSwitch = letter.style.order; // Stores the position of the letter
indexLettertoSwitch = word.indexOf(letter.innerHTML); // Stores the index of the letter
turn ++;
}else if(turn % 2 === 1){ // If its the second time the user clicks on a letter
letterToBeSwitchedWith = letter.style.order; // Stores the position of the letter
turn ++;
}
if(turn % 2 == 0 && turn !== 0){ // If the player has made to moves and its not the first turn
letter.style.order = lettertoSwitch; // Switches the current letter with the first one clicked
letters[indexLettertoSwitch].style.order = letterToBeSwitchedWith; // Switches the second clicked letter with first one clicked
console.log(lettertoSwitch, letterToBeSwitchedWith);
}
})
How can I get the index of the letter through the order property of the element, as it is the only unique property that can distinguish the same letters? You could use the event to access the element index?
It worked! Good logic. Interesting that there is no function to get the index by the style property.
– Inês Barata Feio Borges