jeudi 19 novembre 2020

How to say.(does array[0] equal secondArray[0] or [1] or [2])

Hey so here is an example of what ive written (in javascript)

function checkCardOne(){
 if(animalNums[0].animal.textContent === findArray[0].div.textContent){
     animalNums[0].animal.style.display = "block"
     animalNums[0].animal.classList.add("animate");
     setTimeout(function(){animalNums[0].animal.classList.remove("animate");},1001)
     score += 1
     document.getElementById("scoreP").innerHTML = `Score: ${score}` 
 }
 else if(animalNums[0].animal.textContent === findArray[1].div.textContent){
     animalNums[0].animal.style.display = "block"
     animalNums[0].animal.classList.add("animate");
     setTimeout(function(){animalNums[0].animal.classList.remove("animate");},1001)
     score += 1
     document.getElementById("scoreP").innerHTML = `Score: ${score}` 
 }
 else if(animalNums[0].animal.textContent === findArray[2].div.textContent){
     animalNums[0].animal.style.display = "block"
     animalNums[0].animal.classList.add("animate");
     setTimeout(function(){animalNums[0].animal.classList.remove("animate");},1001)
     score += 1
     document.getElementById("scoreP").innerHTML = `Score: ${score}` 
 }
 else{
     animalNums[0].animal.style.display = "block"
     animalNums[0].animal.classList.add("animate");
     setTimeout(function(){animalNums[0].animal.classList.remove("animate");
     animalNums[0].animal.style.display = "none";},1001
     )
     score -= 1
     document.getElementById("scoreP").innerHTML = `Score: ${score}`
 }
}

I want to slim this down as I have 12 of these functions and my js file is over 700 lines long

I want to be able to say : if(animalNums[0].animal.textContent === findArray[0].textContent || findArray[1].textContent || findArray[2].textContent)

All in one line instead of three else if. Can someone help ive tried afew different things but they haven't worked.

Aucun commentaire:

Enregistrer un commentaire