I have added a click event listener to a button. It calls the buttons YES AND NO. Basically the indexOf checks if the value in the variable foto is in the yesMeetup array or in the notMeetup array.
I tried to debug but I always get "You got it" and it's not calling the debugger when I click on NO button
let foto = Math.floor(Math.random() * 20) + 1;
document.querySelector('.btn').addEventListener('click', verify);
function verify() {
var yesMeetup = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15];
var notMeetup = [16, 17, 18, 19, 20];
var notButton = document.getElementById('no');
var yesButton = document.getElementById('yes');
var decisao = document.getElementById('decisao');
debugger;
if (yesButton) {
if (yesMeetup.indexOf(foto)) {
decisao.textContent = "You got it";
} else if (notMeetup.indexOf(foto)) {
decisao.textContent = "wrong";
}
} else if (notButton) {
if (notMeetup.indexOf(foto)) {
decisao.textContent = "You Gou it";
} else if (yesMeetup.indexOf(foto)) {
decisao.textContent = "Wrong";
}
}
}
fotovariable inside theverify()method. Moreover, bothyesButtonandnoButtonwill return true, assuming the elements exists. What you want is to check the event target to determine which button is clicked. Likee.target.id === 'yes'.yesButtonreturns an element. It will always be atruthyvalue if it exists on the DOM. It should probably beyesButton.value