Skip to content

Commit 770278f

Browse files
committed
Graded Assignment LaunchCodeEducation#3: Mars Rover - all passing tests
1 parent 01bce1e commit 770278f

2 files changed

Lines changed: 90 additions & 48 deletions

File tree

rover.js

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const Command = require("./command");
22

3-
43
class Rover {
54
constructor(position, mode = "NORMAL", generatorWatts = 110) {
65
// Write code here!
@@ -10,8 +9,6 @@ class Rover {
109
}
1110

1211
receiveMessage(message) {
13-
//console.log("RECEIVE MESSAGE:", message);
14-
1512
let results = [];
1613
for (let command of message.commands) {
1714
let result = this.performCommand(command);
@@ -25,51 +22,44 @@ class Rover {
2522

2623
performCommand(command) {
2724
// write some logic for commands here
28-
let results;
29-
//let statusCheckCommand = new Command("STATUS_CHECK");
25+
let results = {}; //maybe I should initialize results with empty object
26+
// console.log("///RESULTS///", results);
27+
3028
if (command.commandType === "STATUS_CHECK") {
3129
let roverStatus = {
3230
position: this.position,
3331
mode: this.mode,
3432
generatorWatts: this.generatorWatts,
35-
3633
};
37-
console.log("ROVER STATUS:", roverStatus);
38-
results = {completed: true, roverStatus: roverStatus};
34+
35+
results = { completed: true, roverStatus: roverStatus };
3936
} else if (command.commandType === "MOVE") {
40-
results = {completed: true};
41-
} else if (command.commandType === "MODE_CHANGE"){
42-
results = {completed: true};
43-
37+
// console.log("///NEW POSITION///", command.value);
38+
let newPosition = command.value; //extract new position value from the command
39+
40+
if (this.mode === "LOW_POWER") {
41+
results.completed = false;
42+
} else {
43+
this.position = newPosition; //update the rover's position with the new position
44+
results.completed = true;
45+
results.roverStatus = {
46+
position: this.position,
47+
mode: this.mode,
48+
generatorWatts: this.generatorWatts,
49+
};
50+
}
51+
} else if (command.commandType === "MODE_CHANGE") {
52+
this.mode = command.value;
53+
results.completed = true;
4454
} else {
4555
console.error(`Unknown command type: ${command.commandType}`);
56+
results.completed = false; //if test command is not completed
57+
}
58+
if (command.commandType === "STATUS_CHECK" && !results.completed) {
59+
console.error("STATUS_CHECK command not found in the message");
4660
}
47-
//console.log("RESULTS:", results);
4861
return results;
4962
}
5063
}
5164

5265
module.exports = Rover;
53-
54-
// Tip from Phillip
55-
56-
// it("Responds to TA message & commands", function() {
57-
// let rover = new Rover(100);
58-
// let commands = [
59-
// new Command('MOVE', 4321),
60-
// new Command('STATUS_CHECK'),
61-
// new Command('MODE_CHANGE', 'LOW_POWER'),
62-
// new Command('MOVE', 3579),
63-
// new Command('STATUS_CHECK')
64-
// ];
65-
// let message = new Message('TA power', commands);
66-
// let response = rover.receiveMessage(message);
67-
// expect(response.message).toEqual('TA power');
68-
// expect(response.results[0].completed).toBeTruthy();
69-
// expect(response.results[1].roverStatus.position).toEqual(4321);
70-
// expect(response.results[2].completed).toBeTruthy();
71-
// expect(response.results[3].completed).toBeFalsy();
72-
// expect(response.results[4].roverStatus.position).toEqual(4321);
73-
// expect(response.results[4].roverStatus.mode).toEqual('LOW_POWER');
74-
// expect(response.results[4].roverStatus.generatorWatts).toEqual(110);
75-
// });

spec/rover.spec.js

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe("Rover class", function () {
4545
"Test message with status check command",
4646
commands
4747
);
48-
console.log("MESSAGE:", message);
48+
// console.log("MESSAGE:", message);
4949
let rover = new Rover(98382);
5050
let response = rover.receiveMessage(message);
5151
let roverStatus = response.results[0].roverStatus;
@@ -57,38 +57,90 @@ describe("Rover class", function () {
5757

5858
// 11 tests here!
5959
test("responds correctly to the mode change command", function () {
60+
let newPosition = 3579;
6061
let commands = [
6162
new Command("MODE_CHANGE", "LOW_POWER"),
62-
new Command("STATUS_CHECK"),
63-
new Command("MOVE"),
63+
new Command("STATUS_CHECK", ""),
64+
new Command("MOVE", newPosition),
6465
];
6566

6667
let message = new Message(
6768
"Test message with mode change command",
6869
commands
6970
);
71+
// console.log("///MESSAGE:///", message);
7072
let rover = new Rover(98382);
71-
7273
let response = rover.receiveMessage(message); // Check if the STATUS_CHECK command is found in the response
7374

7475
let statusCheckResult = response.results.find(
75-
(result) => result.commandType === "STATUS_CHECK"
76+
(result) =>
77+
result &&
78+
result.command &&
79+
result.command.commandType === "STATUS_CHECK"
7680
); // state the rover status if STATUS_CHECK command is found
7781

7882
if (statusCheckResult) {
7983
let roverStatus = statusCheckResult.roverStatus;
8084
expect(roverStatus.mode).toBe("NORMAL");
8185
expect(roverStatus.generatorWatts).toBe(110);
8286
expect(roverStatus.position).toBe(98382);
83-
} else {
84-
// handle if STATUS_CHECK command is not found
85-
console.error("STATUS_CHECK command not found in the message");
8687
}
88+
// else {
89+
// // handle if STATUS_CHECK command is not found
90+
// console.error("STATUS_CHECK command not found in the message");
91+
// }
8792
});
8893

89-
// // 12 tests here!
90-
// test("responds with a false completed value when attempting to move in LOW_POWER mode", function () {});
94+
// 12 tests here!
95+
test("responds with a false completed value when attempting to move in LOW_POWER mode", function () {
96+
// let rover = new Rover(100);
97+
let rover = new Rover(4321); //initial position
98+
let commands = [
99+
new Command("MOVE", 4321), //move to same position
100+
new Command("STATUS_CHECK"),
101+
new Command("MODE_CHANGE", "LOW_POWER"),
102+
new Command("MOVE", 3579), //move to different position
103+
new Command("STATUS_CHECK"),
104+
];
105+
106+
let message = new Message(
107+
"Test message with commands for LOW_POWER mode",
108+
commands
109+
);
110+
let response = rover.receiveMessage(message);
91111

92-
// // 13 tests here!
93-
// test("responds with the position for the move command", function () {});
112+
expect(response.message).toEqual(
113+
"Test message with commands for LOW_POWER mode"
114+
);
115+
expect(response.results[0].completed).toBeTruthy();
116+
expect(response.results[1].roverStatus.position).toEqual(4321);
117+
expect(response.results[2].completed).toBeTruthy();
118+
expect(response.results[3].completed).toBeFalsy();
119+
expect(response.results[4].roverStatus.position).toEqual(4321);
120+
expect(response.results[4].roverStatus.mode).toEqual("LOW_POWER");
121+
expect(response.results[4].roverStatus.generatorWatts).toEqual(110);
122+
});
123+
// 13 tests here!
124+
test("responds with the position for the move command", function () {
125+
// Create a new rover object with an initial position of 4321
126+
let rover = new Rover(4321);
127+
128+
// Define a new position for the move command
129+
let newPosition = 3579;
130+
131+
// Define commands for the message
132+
let commands = [
133+
new Command("MOVE", newPosition), // Move to the new position
134+
new Command("STATUS_CHECK"), // Check rover status
135+
];
136+
137+
// Create a new message with the defined commands
138+
let message = new Message("Test message with move command", commands);
139+
140+
// Receive the message and get the response
141+
let response = rover.receiveMessage(message);
142+
143+
// Test if the rover's position is updated to the new position specified in the MOVE command
144+
expect(response.results[0].roverStatus.position).toEqual(newPosition);
145+
});
94146
});

0 commit comments

Comments
 (0)