5

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";
      }

   }
}
4
  • You are generating the random number outside the click function. In this case, the number is set in stone at runtime and will never be updated again. If you want a new random number to be generated each time a click event is fired, you need to do the assignment of the foto variable inside the verify() method. Moreover, both yesButton and noButton will return true, assuming the elements exists. What you want is to check the event target to determine which button is clicked. Like e.target.id === 'yes'. Commented Feb 23, 2019 at 19:59
  • yesButton returns an element. It will always be a truthy value if it exists on the DOM. It should probably be yesButton.value Commented Feb 23, 2019 at 19:59
  • actually this variable foto needs to get into this function gerar() but as if I put inside it will be just on the scope of this function Idecided to put outside function gerar(){ var score = document.getElementById('score'); var fotoDOM = document.querySelector('.foto'); fotoDOM.src = 'foto-' + foto + '.jpg'; console.log(foto); document.getElementById('photo').style.animation ="appearPerson 1s"; score.innerHTML = foto; foto; } Commented Feb 23, 2019 at 21:22
  • but I have to get this foto variable and compare with the array inside verify(), How can but this foto variable needs to stay inside the function verify. How can I get this variable foto inside the function verify without change the number that is generating inside gerar() ?? Commented Feb 23, 2019 at 21:25

2 Answers 2

4

An if statement will evaluate anything passed into it as a boolean.

The only values for which it will not execute the "true" branch are all falsy values: 0, null, undefined, '', false, NaN.

Array.prototype.indexOf returns -1 when an element is not present in an array, which is not one of the falsy values and thus your if condition

if (array.indexOf(element))

will always evaluate as true.

var example = [1,2,3];
if (example.indexOf(4)) {
  console.log('still true');
}

You can use a direct comparison to -1:

var example = [1,2,3];
if (example.indexOf(4) !== -1) {
  console.log('this is not logged');
}

Or a newer, a bit cleaner, Array.prototype.includes:

var example = [1,2,3];
if (example.includes(4)) {
  console.log('this is not logged');
}

Sign up to request clarification or add additional context in comments.

Comments

0

You can add click events to the buttons and check the values of the random variable. As the way you have written the code, it will always return the values for both the scenarios. Alternatively, to check the values at run time, you can use alert().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.