forked from LaunchCodeEducation/Mars-Rover-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrover.js
More file actions
52 lines (45 loc) · 1.44 KB
/
rover.js
File metadata and controls
52 lines (45 loc) · 1.44 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Rover {
// Write code here!
constructor(position) {
this.position = position;
this.mode = 'NORMAL';
this.generatorWatts = 110;
}
receiveMessage(message) {
let commandOutput = [];
for (const element of message.commands) {
if (element.commandType === 'STATUS_CHECK') {
let roverStatusInput = {
mode: this.mode,
generatorWatts: this.generatorWatts,
position: this.position
}
commandOutput.push({completed: true, roverStatus: roverStatusInput});
} else if (element.commandType === 'MODE_CHANGE') {
if (element.value === 'LOW_POWER') {
this.mode = 'LOW_POWER';
commandOutput.push({completed: true});
} else if (element.value === 'NORMAL') {
this.mode = 'NORMAL';
commandOutput.push({completed: true});
} else {
commandOutput.push({completed: false});
}
} else if (element.commandType === 'MOVE') {
if (this.mode === 'LOW_POWER') {
commandOutput.push({completed: false});
} else if (this.mode === 'NORMAL') {
this.position = element.value;
commandOutput.push({completed: true});
}
} else {
commandOutput.push({completed: false});
}
}
return {
message: message.name,
results: commandOutput
}
}
}
module.exports = Rover;