How can I write a condition that only does something when a certain function is called

How can I write a function that has a condition ( another function being called) and only returns what I want when the otherr function is called ?

In the example below I tried to create a function “IfCalled” which returns a value only when “MoveSnakeRight()” is called.

MoveSnakeRight() is only called when I press a key.

function IfCalled(){
   if ( MoveSnakeRight() )
return BodySnake.style.GridRowStart = x -1;
}
function moveSnakeRight(){
  if ( Initial_x + x < 10)
  return x++ 
;
}

I think you’re over complicating it…

If you only want IfCalled() to run when moveSnakeRight() is called, could you not put the call inside of moveSnakeRight()?

function ifCalled() {
    // do something
}

function moveSnakeRight() {
    // do this function's task...
    ifCalled();
    return ...
}

It looks like you’re using the IfCalled() function to set a property on another object anyway, so this should accomplish the same thing… but it’s a bit difficult to piece together entirely from just those little snippets. :slight_smile:

Hopefully this helps…

2 Likes

Yes , I agree with you. I was overthinking too much and din’t see something obvious.
Thanks :relieved: :relieved:

2 Likes