0

I am very new in javascript and I am lost with this usecase: I have a function :

function1 (trialNumber, next2Pictures){
1- Display 2 pictures 

// 2- When a user clicks on one of the pictures
2-1 write you clicked on picture 1(or 2) 
2-2 increment trialNumber
2-3 choose new 2pictues
2-4 Return [newTrialNumber, new2Pictures]

 }

I have function2 that calls function1 as far as trialNumber didn't exceed totalTrialsNumber. I have this implementation

function2() {
// 1- instantiate trialNumber and  next2Pictures
 // 2- loop
while(trialNumber < totalTrialsNumber) {
[trialNumber, next2Pictures]= function1 (trialNumber, next2Pictures)
console.log("trialNumber incremented properly")
}
}

the problem is I don't know how to properly return values from function1 that can be used in the while loop of function2. I really need to increment the trialNumber only when a user clicks on a picture. I have checked some answers online but still can't figure it out.

Samorix
  • 307
  • 4
  • 17
  • have a look at this, https://stackoverflow.com/questions/27250357/how-to-return-value-from-anonymous-function-in-js – RyDog Dec 23 '19 at 11:26
  • 1
    Since javascript is single threaded, waiting for a return value in a while loop could cause the browser to freeze. Do it using **Event Listener** instead in the image `onclick` event – xxMrPHDxx Dec 23 '19 at 11:27
  • @xxMrPHDxx how can "Event Listener" help me solve my current problem please? – Samorix Dec 23 '19 at 12:41
  • Move the counter into the global scope and assign for each image event listener using `addEventListener('click', function(event){/* Your logic here */})`. Good luck!! – xxMrPHDxx Dec 23 '19 at 12:57

0 Answers0