Mars Rover, Part 3¶
With Part 2 completed, let's turn our attention to the final class, the Rover
class.
Rover
¶
Rover
receives a message object, updates its properties from the message, and
returns the results.
Remember to use TDD by first reading the class description, writing tests, and then coding the class.
Rover Class Description¶
This class builds a rover object with a few properties, and it also contains
a function outside of constructor
to handle updates to its properties.
constructor(position)
position
is a number representing the rover's position.Sets
this.position
toposition
Sets
this.mode
to'NORMAL'
Sets default value for
generatorWatts
to 110
receiveMessage(message)
message
is aMessage
objectReturns an object containing at least two properties:
message
: the name of the originalMessage
objectresults
: an array of results. Each element in the array is an object that corresponds to oneCommand
inmessage.commands
.
Updates certain properties of the rover object
Details about how to respond to different commands are in the Command Types table.
Example
let commands = [new Command('MODE_CHANGE', 'LOW_POWER'), new Command('STATUS_CHECK')];
let message = new Message('Test message with two commands', commands);
let rover = new Rover(98382); // Passes 98382 as the rover's position.
let response = rover.receiveMessage(message);
console.log(response);
Output
{
message: 'Test message with two commands',
results: [
{
completed: true
},
{
completed: true,
roverStatus: { mode: 'LOW_POWER', generatorWatts: 110, position: 98382 }
}
]
}
Rover
Tests¶
Create spec/rover.spec.js
and write the following tests. Write the code to
make them pass in rover.js
. Remember to use the given phrase as the test
description.
Test 7¶
"constructor sets position and default values for mode and generatorWatts". Refer to the Rover Class description above for these default values.
Test 8¶
"response returned by receiveMessage contains name of message"
Test 9¶
"response returned by receiveMessage includes two results if two commands are sent in the message"
Test 10¶
"responds correctly to status check command"
For the
STATUS_CHECK
command,receiveMessage(message).results
includes aroverStatus
object describing the current state of the rover object ---mode
,generatorWatts
, andposition
. The test should check each of these for accuracy.See the Rover Command Types table for more details.
Test 11¶
"responds correctly to mode change command".
The test should check the
completed
property and rover mode for accuracy.The rover has two modes that can be passed a values to a mode change command, 'LOW_POWER' and 'NORMAL'.
Test 12¶
"responds with false completed value when attempting to move in LOW_POWER mode".
The test should check the
completed
property for accuracy and confirm that the rover position did not change.Use the Rover Modes table for guidance on how to handle move commands in different modes.
Test 13¶
"responds with position for move command".
A
MOVE
command will update the rover's position with the position value in the command.
Rover Command Types¶
Command |
Value sent with command |
Updates to |
Result returned |
---|---|---|---|
MOVE |
Number representing the position the rover should move to. |
|
|
STATUS_CHECK |
No values sent with this command. |
No updates |
|
MODE_CHANGE |
String representing rover mode (see modes) |
|
|
Note
The response value for completed
will be false
if the command could
NOT be completed.
Rover Modes¶
Mode |
Restrictions |
---|---|
LOW_POWER |
Can't be moved in this state. |
NORMAL |
None |
Once your Rover
class is complete, make sure to submit your work.