forked from LaunchCodeEducation/Mars-Rover-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrover.js
37 lines (36 loc) · 1.18 KB
/
rover.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Rover {
constructor(position) {
this.position = position;
this.mode = "NORMAL";
this.generatorWatts = 110;
}
receiveMessage(messageGiven) {
let outputObject = { message: messageGiven.name, results: [] };
for (let i = 0; i < messageGiven.commands.length; i++) {
if (messageGiven.commands[i].commandType === "MODE_CHANGE") {
this.mode = messageGiven.commands[i].value;
outputObject.results.push({ completed: true });
} else if (messageGiven.commands[i].commandType === "MOVE") {
if (this.mode === "LOW_POWER") {
outputObject.results.push({ completed: false });
} else {
this.position = messageGiven.commands[i].value;
outputObject.results.push({ completed: true });
}
} else if (messageGiven.commands[i].commandType === "STATUS_CHECK") {
outputObject.results.push({
completed: true,
roverStatus: {
mode: this.mode,
generatorWatts: this.generatorWatts,
position: this.position,
},
});
} else {
return `Invalid command input.`;
}
}
return outputObject;
}
}
module.exports = Rover;