jeudi 20 mai 2021

Can pass a conditional statement as a function argument?

I have a main function and want to control 2 actions with it. Thereby I pass different variables to the function.

My question is whether I can also pack the condition of an If query into a variable.

For the one action I want to have CurrentImageNumber >= 2 as an if argument and for the other action CurrentImageNumber <= 4 in the if query (Note: CurrentImageNumber outputs a number).

The code would look something like this:

// Declaration of the variables simplified for readability reasons

doStuff(a = CurrentImageNumber >= 2) // When a button is pressed, this variable is used
doStuff(a = CurrentImageNumber <= 4) // Another button activates this one

function doStuff() {

  if (a) {
  // Execute some fancy code
  }
}

Aucun commentaire:

Enregistrer un commentaire