-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrover.js
63 lines (58 loc) · 2.13 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Rover {
constructor(position) {
this.position = position;
this.mode = 'NORMAL';
this.generatorWatts = 110;
}
receiveMessage(message) {
this.message = message;
let response = {
message: message.name,
results: []
}
// Loops through the each command inside the message command property
for (let command of message.commands) {
if (command.commandType === "STATUS_CHECK") {
response.results.push({
completed: true,
roverStatus: {
mode: this.mode,
generatorWatts: this.generatorWatts,
position: this.position
}
})
} else if (command.commandType === "MODE_CHANGE") {
this.mode = command.value,
response.results.push({
completed: true
})
} else if ((command.commandType === "MOVE") && (this.mode === "LOW_POWER")) {
response.results.push({
completed: false
})
} else if ((command.commandType === "MOVE") && (this.mode === "NORMAL")) {
this.position = command.value,
response.results.push({
completed: true
})
}
}
return response;
}
}
module.exports = Rover;
/*
>> Rover Class Description <<
constructor(position)
position is a number representing the rover's position.
Sets this.position to position
Sets this.mode to 'NORMAL'
Sets default value for generatorWatts to 110
receiveMessage(message)
message is a Message object
Returns an object containing at least two properties:
message: the name of the original Message object
results: an array of results. Each element in the array is an object that corresponds to one Command in message.commands.
Updates certain properties of the rover object
Details about how to respond to different commands are in the Command Types table.
*/