jeudi 19 novembre 2020

Error: "cannot read property "div" of undefined"

I keep getting this error

"Cannot read property 'div' of undefined"

This is my Code


function checkCard(){
    for(let i = 0; i < 13; i++){
        if(animalNums[i].animal.textContent === findArray[i].div.textContent){
            animalNums[i].animal.style.display = "block"
            animalNums[i].animal.classList.add("animate");
            setTimeout(function(){animalNums[i].animal.classList.remove("animate");},1001)
            score += 1
            document.getElementById("scoreP").innerHTML = `Score: ${score}` 
        }
    }
}

Its saying findArray[i].div is undefined here is the array it is referencing :

let findArray = [ 
    {
       div: document.getElementById("findOne")
    },
    {
       div: document.getElementById("findTwo")
    },
    {
       div: document.getElementById("findThree")
    }
]

I'm not sure why this is happening as I believe I have done a very similar function that worked before. Is this something to do with Scope?

Aucun commentaire:

Enregistrer un commentaire