# JavaScript Tutorial Through Katas: Mars Rover

A programming kata is an exercise which helps a programmer hone his skills through practice and repetition.

The article assumes that the reader is familiar with the basic usage of JavaScript and Jasmine asserts and knows how to run them from his favorite IDE (mine is WebStorm or IntelliJ IDEA).

Tests that prove that the solution is correct are displayed below. Recommended way to solve this kata is to write the implementation for the first test, confirm that it passes and move to the next. Once all of the tests pass, the kata can be considered solved.

One possible solution is provided below the tests. Try to solve the kata by yourself first.

# Mars Rover

Develop an api that moves a rover around on a grid.

• You are given the initial starting point (x,y) of a rover and the direction (N,S,E,W) it is facing.
• The rover receives a character array of commands.
• Implement commands that move the rover forward/backward (f,b).
• Implement commands that turn the rover left/right (l,r).
• Implement wrapping from one edge of the grid to another. (planets are spheres after all)
• Implement obstacle detection before each move to a new square. If a given sequence of commands encounters an obstacle, the rover moves up to the last possible point and reports the obstacle.

Following is a set of unit tests that can be used to solve this kata in the TDD fashion.

 describe('Mars Rover', function() { describe('You are given the initial starting point (x,y) of a rover and the direction (N,S,E,W) it is facing', function() { it('should set starting location', function() { var mr = new MarsRover([12, 21]); expect(mr.location).toEqual([12, 21]); }); it('should use default starting location value 0x0 when not assigned', function() { var mr = new MarsRover(); expect(mr.location).toEqual([0, 0]); }); it('should set direction as numeric value', function() { var mr = new MarsRover([12, 21], 'N'); expect(mr.direction).toEqual('N'); }); it('should use default starting direction value N when not assigned', function() { var mr = new MarsRover([12, 21]); expect(mr.direction).toEqual('N'); }); }); describe('The rover receives a character array of commands', function() { it('should set commands array', function() { var mr = new MarsRover([12, 21], 'N'); mr.commands(['do', 'this', 'and', 'then', 'do', 'that']); expect(mr.commands()).toEqual(['do', 'this', 'and', 'then', 'do', 'that']); }); }); describe('Implement commands that move the rover forward/backward (f,b)', function() { it('should reduce Y when moving north', function() { var mr = new MarsRover([12, 21], 'N'); mr.commands(['f', 'f']); expect(mr.location).toEqual([12, 19]); }); it('should increase Y when moving south', function() { var mr = new MarsRover([12, 21], 'S'); mr.commands(['f']); expect(mr.location).toEqual([12, 22]); }); it('should reduce X when moving west', function() { var mr = new MarsRover([12, 21], 'W'); mr.commands(['f']); expect(mr.location).toEqual([11, 21]); }); it('should increase X when moving east', function() { var mr = new MarsRover([12, 21], 'E'); mr.commands(['f']); expect(mr.location).toEqual([13, 21]); }); it('should increase Y when moving backwards facing north', function() { var mr = new MarsRover([12, 21], 'N'); mr.commands(['b']); expect(mr.location).toEqual([12, 22]); }); it('should reduce Y when moving backwards facing south', function() { var mr = new MarsRover([12, 21], 'S'); mr.commands(['b']); expect(mr.location).toEqual([12, 20]); }); it('should increase X when moving backwards facing west', function() { var mr = new MarsRover([12, 21], 'W'); mr.commands(['b']); expect(mr.location).toEqual([13, 21]); }); it('should reduce X when moving backwards facing east', function() { var mr = new MarsRover([12, 21], 'E'); mr.commands(['b']); expect(mr.location).toEqual([11, 21]); }); }); describe('Implement commands that turn the rover left/right (l,r)', function() { it('should change direction from E to N when command is to turn left', function() { var mr = new MarsRover([12, 21], 'E'); mr.commands(['l']); expect(mr.direction).toEqual('N'); }); it('should change direction from N to W when command is to turn left', function() { var mr = new MarsRover([12, 21], 'N'); mr.commands(['l']); expect(mr.direction).toEqual('W'); }); it('should change direction from E to S when command is to turn right', function() { var mr = new MarsRover([12, 21], 'E'); mr.commands(['r']); expect(mr.direction).toEqual('S'); }); }); describe('Implement wrapping from one edge of the grid to another (planets are spheres after all)', function() { it('should assign grid size', function() { var mr = new MarsRover([12, 21], 'N', [12, 33]); expect(mr.grid).toEqual([12, 33]); }); it('should use default value 100x100 when grid is not assigned', function() { var mr = new MarsRover([12, 21], 'N'); expect(mr.grid).toEqual([100, 100]); }); it('should return X to 0 when grid is passed', function() { var mr = new MarsRover([9, 9], 'E', [10, 10]); mr.commands(['f']); expect(mr.location).toEqual([0, 9]); }); it('should return Y to 0 when grid is passed', function() { var mr = new MarsRover([9, 9], 'S', [10, 10]); mr.commands(['f']); expect(mr.location).toEqual([9, 0]); }); it('should return X to grid end when grid is passed from west', function() { var mr = new MarsRover([0, 9], 'E', [10, 10]); mr.commands(['b']); expect(mr.location).toEqual([9, 9]); }); it('should return Y to grid end when grid is passed from north', function() { var mr = new MarsRover([9, 0], 'N', [10, 10]); mr.commands(['f']); expect(mr.location).toEqual([9, 9]); }); }); describe('Implement obstacle detection before each move to a new square.' + ' If a given sequence of commands encounters an obstacle,' + ' the rover moves up to the last possible point and reports the obstacle', function() { it('should assign obstacles', function() { var mr = new MarsRover([12, 21], 'N', [12, 33], [[5, 5], [3, 7]]); expect(mr.obstacles).toEqual([[5, 5], [3, 7]]); }); it('should use empty array when obstacles are not assigned', function() { var mr = new MarsRover([12, 21], 'N'); expect(mr.obstacles.length).toEqual(0); }); it('should not move to the obstacle', function() { var mr = new MarsRover([0, 0], 'E'); mr.obstacles = [[5, 1], [3, 0]]; mr.commands(['f', 'f', 'f']); expect(mr.location).toEqual([2, 0]); }); it('should stop when obstacle is detected', function() { var mr = new MarsRover([0, 0], 'E'); mr.obstacles = [[3, 0]]; mr.commands(['f', 'f', 'f', 'l', 'f']); expect(mr.location).toEqual([2, 0]); }); it('should set status to obstacle when one is detected', function() { var mr = new MarsRover([0, 0], 'E'); mr.obstacles = [[1, 0]]; mr.commands(['f']); expect(mr.status).toEqual('obstacle'); }); it('should leave status to OK when obstacle is NOT detected', function() { var mr = new MarsRover([0, 0], 'E'); mr.commands(['f']); expect(mr.status).toEqual('OK'); }); }); });
view raw MarsRover.spec.js hosted with ❤ by GitHub

One possible solution is following.

 function MarsRover(location, direction, grid, obstacles) { self = this; this.location = (location === undefined) ? [0, 0] : location; this.direction = (direction === undefined) ? 'N' : direction; this.grid = (grid === undefined) ? [100, 100] : grid; this.obstacles = (obstacles === undefined) ? [] : obstacles; this.status = 'OK'; this.commands = function(commands) { if (commands === undefined) { // Getter return this.commandsArray; } else { // Setter for(var index = 0; index < commands.length; index++) { var command = commands[index]; if (command === 'f' || command === 'b') { if (!move(command)) break; } else if (command === 'l' || command === 'r') { turn(command); } } resetLocation(); this.commandsArray = commands; } }; function resetLocation() { self.location = [ (self.location + self.grid) % self.grid, (self.location + self.grid) % self.grid ] } function move(command) { var xIncrease = 0, yIncrease = 0; if (self.direction === 'N') { yIncrease = -1; } else if (self.direction === 'E') { // East xIncrease = 1; } else if (self.direction === 'S') { // South yIncrease = 1; } else if (self.direction === 'W') { // West xIncrease = -1; } if (command === 'b') { // Backward xIncrease *= -1; yIncrease *= -1; } var newLocation = [self.location + xIncrease, self.location + yIncrease]; if (isObstacle(newLocation)) { return false; } self.location = newLocation; return true; } function isObstacle(newLocation) { for(var index = 0; index < self.obstacles.length; index++) { if (newLocation.toString() == self.obstacles[index].toString()) { self.status = 'obstacle'; return true; } } return false; } function turn(command) { var directionNumber = directionAsNumber(self.direction); if (command === 'l') { // Left directionNumber = (directionNumber + 4 - 1) % 4; } else { // Right directionNumber = (directionNumber + 1) % 4; } self.direction = self.directions[directionNumber]; } this.directions = ['N', 'E', 'S', 'W']; function directionAsNumber(direction) { for(var index = 0; index < 4; index++) { if (self.directions[index] === direction) return index; } } }
view raw MarsRover.js hosted with ❤ by GitHub

Full source is located in the GitHub repo https://github.com/vfarcic/mars-rover-kata-java-script. Besides tests and implementation, it includes package.json with NodeJS dependencies, Bower configuration with JS libraries and Grunt configuration that can be used run tests. README.md contains short instructions how to setup the project.

There are many ways to improve both tests and implementation of the provided solution. We could, for example:

• Check whether object values are correct. Location and grid must be number arrays consisting of two numbers.
• Check whether object values are correct. Direction must be one of following values: 'N', 'S', 'E', 'W'
• Check whether command values are correct Commands must be an arrays consisting of one of following values: 'f', 'b', 'l', 'r'.
• Change the code so that it works with both lower and upper cases (i.e 'N' or 'n' to specify north).

What was your solution? Post it as a comment so that we can compare different ways to solve this kata.

## 1 thought on “JavaScript Tutorial Through Katas: Mars Rover”

1. Alex

How do you run the tests?