Finished
1111}
1212
1313export class Robot {
14 constructor() {
15 this._bearing = 'north';
16 this._coordinates = [0, 0];
17 this._directions = ['north', 'east', 'south', 'west'];
18 }
19
1420 /**
1521 * @returns {string}
1622 */
1723 get bearing() {
18 throw new Error('Remove this line and implement the function');
24 return this._bearing;
1925 }
2026
2127 /**
2228 * @returns {number[]}
2329 */
2430 get coordinates() {
25 throw new Error('Remove this line and implement the function');
31 return this._coordinates;
2632 }
2733
2834 /**
2935 * @param {{x: number, y: number, direction: string}} position
3036 */
3137 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];
3350 }
3451
3552 /**
3653 * @param {string} instructions
3754 */
3855 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 }
40108 }
41109}
110
111// Handled Edge Cases: Empty instruction string, Invalid instruction characters, Invalid direction, Non-numeric coordinates
Test NameStatus
facing north by default
Pass
facing east
Pass
facing west, at origin
Pass
at negative position facing south
Pass
invalid robot bearing
Pass
changes north to east
Pass
changes east to south
Pass
changes south to west
Pass
changes west to north
Pass
changes north to west
Pass
changes west to south
Pass
changes south to east
Pass
changes east to north
Pass
advance when facing north
Pass
advance when facing south
Pass
advance when facing east
Pass
advance when facing west
Pass
moving east and north from README
Pass
moving west and north
Pass
moving west and south
Pass
moving east and north
Pass
instruct many robots
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.