| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | export class Robot { |
| 14 | constructor() { |
| 15 | this._bearing = 'north'; |
| 16 | this._coordinates = [0, 0]; |
| 17 | this._directions = ['north', 'east', 'south', 'west']; |
| 18 | } |
| 19 | |
| 14 | 20 | /** |
| 15 | 21 | * @returns {string} |
| 16 | 22 | */ |
| 17 | 23 | get bearing() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 24 | return this._bearing; |
| 19 | 25 | } |
| 20 | 26 | |
| 21 | 27 | /** |
| 22 | 28 | * @returns {number[]} |
| 23 | 29 | */ |
| 24 | 30 | get coordinates() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 31 | return this._coordinates; |
| 26 | 32 | } |
| 27 | 33 | |
| 28 | 34 | /** |
| 29 | 35 | * @param {{x: number, y: number, direction: string}} position |
| 30 | 36 | */ |
| 31 | 37 | place({ x, y, direction }) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 38 | // Edge Case: Invalid direction |
| 39 | if (!this._directions.includes(direction)) { |
| 40 | throw new InvalidInputError('Invalid Robot Bearing'); |
| 41 | } |
| 42 | |
| 43 | // Edge Case: Non-numeric coordinates |
| 44 | if (typeof x !== 'number' || typeof y !== 'number') { |
| 45 | throw new InvalidInputError('Invalid coordinates'); |
| 46 | } |
| 47 | |
| 48 | this._bearing = direction; |
| 49 | this._coordinates = [x, y]; |
| 33 | 50 | } |
| 34 | 51 | |
| 35 | 52 | /** |
| 36 | 53 | * @param {string} instructions |
| 37 | 54 | */ |
| 38 | 55 | evaluate(instructions) { |
| 39 | | throw new Error('Remove this line and implement the function'); |
| 56 | // Edge Case: Empty instruction string |
| 57 | if (instructions === '') { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | // Edge Case: Invalid instruction characters |
| 62 | for (const char of instructions) { |
| 63 | if (char !== 'R' && char !== 'L' && char !== 'A') { |
| 64 | throw new InvalidInputError('Invalid instruction'); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | for (const instruction of instructions) { |
| 69 | switch (instruction) { |
| 70 | case 'R': |
| 71 | this._turnRight(); |
| 72 | break; |
| 73 | case 'L': |
| 74 | this._turnLeft(); |
| 75 | break; |
| 76 | case 'A': |
| 77 | this._advance(); |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | _turnRight() { |
| 84 | const currentIndex = this._directions.indexOf(this._bearing); |
| 85 | this._bearing = this._directions[(currentIndex + 1) % 4]; |
| 86 | } |
| 87 | |
| 88 | _turnLeft() { |
| 89 | const currentIndex = this._directions.indexOf(this._bearing); |
| 90 | this._bearing = this._directions[(currentIndex + 3) % 4]; // Adding 3 is equivalent to subtracting 1 |
| 91 | } |
| 92 | |
| 93 | _advance() { |
| 94 | switch (this._bearing) { |
| 95 | case 'north': |
| 96 | this._coordinates[1] += 1; |
| 97 | break; |
| 98 | case 'east': |
| 99 | this._coordinates[0] += 1; |
| 100 | break; |
| 101 | case 'south': |
| 102 | this._coordinates[1] -= 1; |
| 103 | break; |
| 104 | case 'west': |
| 105 | this._coordinates[0] -= 1; |
| 106 | break; |
| 107 | } |
| 40 | 108 | } |
| 41 | 109 | } |
| 110 | |
| 111 | // Handled Edge Cases: Empty instruction string, Invalid instruction characters, Invalid direction, Non-numeric coordinates |