Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. describe('Rover', () => {
  2.  
  3. describe('Turn left', () => {
  4. it('North -> L -> West', () => {
  5. expect(turn('N', 'L')).toBe('W');
  6. });
  7.  
  8. it('West -> L -> South', () => {
  9. expect(turn('W', 'L')).toBe('S');
  10. });
  11.  
  12. it('South -> L -> East', () => {
  13. expect(turn('S', 'L')).toBe('E');
  14. });
  15.  
  16. it('East -> L -> North', () => {
  17. expect(turn('E', 'L')).toBe('N');
  18. });
  19. });
  20.  
  21. describe('Turn right', () => {
  22. it('East -> R -> South', () => {
  23. expect(turn('E', 'R')).toBe('S');
  24. });
  25.  
  26. it('South -> R -> West', () => {
  27. expect(turn('S', 'R')).toBe('W');
  28. });
  29.  
  30. it('West -> R -> North', () => {
  31. expect(turn('W', 'R')).toBe('N');
  32. });
  33.  
  34. it('North -> R -> East', () => {
  35. expect(turn('N', 'R')).toBe('E');
  36. });
  37. });
  38.  
  39. describe('Moving', () => {
  40. it('1 1 N -> M -> 1 2 N', () => {
  41. let newPosition = nextPosition({ x:1, y:1, orientation:'N' }, 'M');
  42. expect(newPosition).toEqual({x:1, y:2, orientation:'N'});
  43. });
  44.  
  45. it('1 1 E -> M -> 2 1 E', () => {
  46. let newPosition = nextPosition({ x:1, y:1, orientation:'E' }, 'M');
  47. expect(newPosition).toEqual({x:2, y:1, orientation:'E'});
  48. });
  49.  
  50. it('1 1 E -> L -> 1 1 N', () => {
  51. let newPosition = nextPosition({ x:1, y:1, orientation:'E' }, 'L');
  52. expect(newPosition).toEqual({x:1, y:1, orientation:'N'});
  53. });
  54.  
  55. });
  56.  
  57. });
  58.  
  59. const orientations = ['N','W','S','E'];
  60. const moves = {
  61. N: position => {
  62. return {
  63. 'x': position.x,
  64. 'y': position.y + 1,
  65. orientation: 'N'
  66. }
  67. },
  68. E: position => {
  69. return {
  70. 'x': position.x + 1,
  71. 'y': position.y,
  72. orientation: 'E'
  73. }
  74. }
  75. };
  76.  
  77. function turn(currentOrientation, command) {
  78. let index = orientations.indexOf(currentOrientation);
  79. let nextIndex = command === 'R' ? index -1 : index + 1;
  80. return orientations[(nextIndex + 4) % 4];
  81. }
  82.  
  83. function nextPosition(currentPosition) {
  84. return move(currentPosition);
  85. }
  86.  
  87. function move(currentPosition) {
  88. return moves[currentPosition.orientation](currentPosition);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement