Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- describe('Rover', () => {
- describe('Turn left', () => {
- it('North -> L -> West', () => {
- expect(turn('N', 'L')).toBe('W');
- });
- it('West -> L -> South', () => {
- expect(turn('W', 'L')).toBe('S');
- });
- it('South -> L -> East', () => {
- expect(turn('S', 'L')).toBe('E');
- });
- it('East -> L -> North', () => {
- expect(turn('E', 'L')).toBe('N');
- });
- });
- describe('Turn right', () => {
- it('East -> R -> South', () => {
- expect(turn('E', 'R')).toBe('S');
- });
- it('South -> R -> West', () => {
- expect(turn('S', 'R')).toBe('W');
- });
- it('West -> R -> North', () => {
- expect(turn('W', 'R')).toBe('N');
- });
- it('North -> R -> East', () => {
- expect(turn('N', 'R')).toBe('E');
- });
- });
- describe('Moving', () => {
- it('1 1 N -> M -> 1 2 N', () => {
- let newPosition = nextPosition({ x:1, y:1, orientation:'N' }, 'M');
- expect(newPosition).toEqual({x:1, y:2, orientation:'N'});
- });
- it('1 1 E -> M -> 2 1 E', () => {
- let newPosition = nextPosition({ x:1, y:1, orientation:'E' }, 'M');
- expect(newPosition).toEqual({x:2, y:1, orientation:'E'});
- });
- it('1 1 E -> L -> 1 1 N', () => {
- let newPosition = nextPosition({ x:1, y:1, orientation:'E' }, 'L');
- expect(newPosition).toEqual({x:1, y:1, orientation:'N'});
- });
- });
- });
- const orientations = ['N','W','S','E'];
- const moves = {
- N: position => {
- return {
- 'x': position.x,
- 'y': position.y + 1,
- orientation: 'N'
- }
- },
- E: position => {
- return {
- 'x': position.x + 1,
- 'y': position.y,
- orientation: 'E'
- }
- }
- };
- function turn(currentOrientation, command) {
- let index = orientations.indexOf(currentOrientation);
- let nextIndex = command === 'R' ? index -1 : index + 1;
- return orientations[(nextIndex + 4) % 4];
- }
- function nextPosition(currentPosition) {
- return move(currentPosition);
- }
- function move(currentPosition) {
- return moves[currentPosition.orientation](currentPosition);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement