I'm learning JavaScript with the JSDares.com. There are several tasks, where a sequences of commands is duplicated in backward order. For example:
function visitCulDeSac(n){
robot.turnRight();
robot.drive(n);
robot.turnLeft();
robot.turnLeft();
robot.drive(n);
robot.turnRight();
}
We can see that the first three commands are "mirrored" in the next three lines.
- The question: Is there any simple way to let Javascript to execute a sequence of code lines in reverse order to avoid the repetition?
I've tried to make an array from the sequence of commands, but JSDares doesn't let to create long strings.
Put the commands in an array of functions, something like this. You can then loop through them to execute them, and change the array to if you want to do it in a different order using the array methods.