issue with checking correct words memory game

Viewed 16

User has to click a word from first array "words" when words are displayed from second array "words1". There is a separate place when those words were shown to memorize. On clicking words from "words1", it says no even if given word was present in "words".


<div id="x"> </div>

<script>
var words1 = ["Shoe", "Hat", "Book", "Paper", "Chair", "Desk", "Apple", "Peach", "Cup", "Spoon", "Rock", "Stone", "Tree", "Flower", "Hair", "Hand", "Bird", "Animal", "Fire", "Water"];
var i = 0;

(function loop() {
    if (i < words1.length) {
        setTimeout(loop, 2000);
        document.getElementById('x').innerHTML = words1[i++];
        document.getElementById('x').onclick = function() {checkifpresent()};
        function checkifpresent() {
        
        var words = ["Shoe", "Book", "Chair", "Apple", "Cup", "Rock", "Tree", "Hair", "Bird", "Fire"];
        if (words.includes('x')){
                  document.getElementById("x").innerHTML = "Yes!";
        } else {
          document.getElementById("x").innerHTML = "No!";
        }
          
        }
      }
    else {
    alert ("Thank you");
    }
})();

</script>
0 Answers
Related