In my example I am trying to loop through a collection of HTML elements and check to see if they match a button click on screen. But the loop only seems to check the first html element and does not run the function again if another button is clicked.
keyboard.onclick = function(event) {
event.target.className += " chosen";
let letterFound = event.target.innerHTML;
checkLetter(letterFound);
}
const checkLetter = (a) => {
let letterCheck = document.querySelectorAll('.letter');
for (i = 0; i < letterCheck.length; i++) {
if (a === letterCheck[i].innerHTML) {
letterCheck[i].className += " show";
let letterMatch = letterCheck[i];
return letterMatch
} else {
return null;
}
}
}
returnstatements in your loop. They cause the enclosing function to exit.