Calculate the minimum number of commands to return to starting point JS – I can’t figure this out

An input string controls the movement of a robot, “F” means move 1 step forward, “L” means turn 90 degrees to the left and “R” means turn 90 degrees to the right. E.g. “FLF” moves the robot one step forward, then turns it left by 90 degrees, then moves the robot one step forward.

Write a function that returns the minimum number of commands that the robot needs to return to it’s starting point after following all the input commands. You should return any characters that are not capital F, L, or R. HINT: You may wish to track the robots position using (X,Y) coordinates, assuming it starts at (0,0).

Example:

. “RF” returns 3 (robot must turn twice and move forward one step (e.g. “RRF”)
. “LFRFRFR” returns 1 (robot must step forward to return )
. “FxLxLxFx” returns 0 (robot is already back at starting point )

Execution time limit is 3 secs
Input: string directions
Output: integer

I had this interview question and I passed 7/10 tests with a long-winded approach – I couldn’t copy my code from their IDE. But I tracked the movements successfully, got the right coordinates, and the steps back were easy – (the sum of the absolute values of the x and y coordinates). But then struggled to figure out how I would factor in the minimum amount of commands when considering the direction changes. Can anyone help please? 🙂