LC101-Ind - Assignment 3 - Mars Rover

I have completed Rover class - Part 3 thru passed Test 9; failing on Test 10 "responds correctly to status check command’. I am thinking it should be helpful to look at studentgrade.spec for how to set-up my tests also. So far - I have response object (thru 2b) that prints out as below from rover.js and cannot complete 2c.
{
message: ‘Test message with five commands for status check’,
results: [
Command { commandType: ‘MOVE’, value: 4321 },
Command { commandType: ‘STATUS_CHECK’, value: undefined },
Command { commandType: ‘MODE_CHANGE’, value: ‘LOW_POWER’ },
Command { commandType: ‘MOVE’, value: 3579 },
Command { commandType: ‘STATUS_CHECK’, value: undefined }
]
}
For Part 3 - 2c. Update certain properties of the rover object.
I iterate thru this response object with a for … in using (this works I believe):
for(let i = 0; i < response.results.length; i++){
but I cannot figure out how to or if:

  1. do a conditional if to see if there is a STATUS_CHECK commandType (from Command class) in each iteration of the for… in for each Command; then set roverStatus.
    I experimented with numerous ways to get to commandType thru an if stmt in rover.js (after above for…in) with no success.
  2. how to successfully include a roverStatus object in (not separate) response.result object also. I assumed I need to do this because studentgrade.spec test STATUS_CHECK example is as follows: expect(response.results[4].roverStatus.position).toEqual(4321);
  3. In rover.js -
    I experimented with importing the Message and Command classes (same level as) into Rover class and decided I do not need to do this - is this correct?
    Has anyone gotten to Test 10 who can let me know if I am on the right track (I have been stuck most of the day) or where I am going wrong. Any hints appreciated. Thanks.

I am hung up on test 8 for days now… I can’t seem to wrap my head around where these details for receiveMessage() are coming from exactly.

Hi Ebbie,

So, you’re definitely on the right track.

Is this object what your Rover.receiveMessage method outputs? Because it looks to match the structure of the Message class. The Rover.receiveMessage method is supposed to output a similar, but distinct object representing the various results of a given commandType outlined here. This might be part of what’s giving your trouble.

You definitely need to iterate over the commands the rover is given, and then construct the object the receiveMessage method will return based off of the commands given (which, according to the Message class will come in the form of a commands array looking something like message.commands).

Conditionals to check each commandType value of every command object is spot on. What specific errors are you receiving when you try to evaluate each object’s commandType? You also mention a for… in loop, but I only see a for loop in your post. Are you iterating across the message object Rover receives with a for… in loop as well?

The roverStatus object is part of the response to a STATUS_CHECK command, and is something you’ll need to build within the receiveMessage method within whatever conditional you’re using to address a STATUS_CHECK commandType. I just went with an object literal with keys corresponding to mode, generatorWatts, and position, and then assigned it to the key “roverStatus” in the command object I was building, which ultimately went into the results array of my return object.

This is correct. You shouldn’t need to import those classes directly into rover.js. Your Rover class only interacts with those other classes via its receiveMessage() method. The method takes an object as its only parameter. Rover doesn’t care what the name or class of that object is, only that it has the keys/properties receiveMessage() expects to work with (i.e. ‘name’ and ‘commands’).

Hope all of that helps! You got it, keep at it!

Hi lizzymae2001,

receiveMessage() takes one parameter, a message object. The details you need for test 8-13 come from the message object Rover receives via receiveMessage(). More specifically, Rover.receiveMessage is intended to take a message object structured from our Message class, which has a name and then commands array.

Hope that helps!

Thanks… That’s how I have it. I guess the question I should have asked is how am I accessing that message object from another node? I don’t understand where this information will be input to begin with and it has my brain rattled. I keep thinking about the big picture and can’t get past it.

The spec file has access to all of your classes via the ‘require’ statements, and to test receiveMessage() and its functionality, you’ll create a message object to use with a given test, then pass that message directly to your rover’s receiveMessage() method in the test. That’s how Rover will get access to the message object.

That said, Rover itself doesn’t care what object it’s passed as an argument, so long as the argument it receives operates as the receiveMessage() expects, and can respond to whatever code you have in your method.

Does that better answer your question?

ok- so I’ve been feeling ok about my code in the rover spec file:
it(“response returned by receiveMessage contains name of message”, function(){
let message = new Message(‘Test message with two commands’);
expect(rover.receiveMessage(message).name).toEqual(‘Test message with two commands’);
});

I guess I don’t know where to go with the receiveMessage() function. I’ve tried some things and can’t get that test to pass. I’m mostly stuck at return message.name because I’m not sure what else I would do just to return a name. I know I will need more variables in this function. Do I need to create another variable for the name?

Hi @lizzymae2001 and @jwrounds, I’m also currently stuck on Test 8.

Currently my rover spec file has a bit more than yours @lizzymae2001, but I’m not sure if it’s correct. I define commands, message, rover and response (as it shown in some of the examples throughout the instructions) and then conclude with expect(response).toContain(“Test message with two commands”);

My code for the function is in the screen shot below, and I’m getting the failure/error message:
“Spec: Rover class response returned by receiveMessage contains name of message was failed
Failure: ReferenceError: item is not defined”.

Do either of you know why it’s not accepting ‘item’ as part of the object iteration?

Screen Shot 2021-03-14 at 2.54.52 PM

@emgraham - A for/in loops through object (v. array) properties. If message is not an object, this is why you may be getting item undefined. I started with a for/in loop also then noticed that my message variable includes a message.commands array that can be iterated thru using a for loop with array indexes (v. object items).

@lizzymae2001 - I used the expect statements in Studentgrading.spec as a guide for my rover.spec.js; then tried to code rover.js toward them. My Assignment 3 now has GitHub passed with green check (thanks to @jwrounds) but I don’t feel it’s DRY as there are probably a number of ways to code to solution.

Hi @Ebbie thank you - but the only thing is that ‘message’ is the first property in the Message object that is taken as a parameter into receiveMessage(); it’s not part of the commands array (which is the second property in the Message object) so I think I have to use the for/in loop in order to access it. My test in rover.spec.js makes ‘message’ an object - so I’m just still not sure on this one. Will just have to keep mulling it over, thanks again for trying to help

You could access the properties of the message object using a for… in loop, but you know both properties by name based on message.js (name and commands), so you can just access those properties directly using dot or bracket notation in your receiveMessage method.

@jwrounds, that’s it, thank you! I was thinking the only way to access the object was with a for/in loop.

I am suffering some severe imposter syndrome right now working on test 8. Even after watching the videos and looking through the help on here, I can’t seem to wrap my head around getting rover.receiveMessage(message) to return message.name. It seems like it should be simple. Am I directly accessing message.js in the method? Or am I making it generic? Anything I try ends up with the test saying that rover.receiveMessage(message) is undefined. Thanks for all the help so far.

1 Like

I was stuck on this one for a long time too. Not sure what your code looks like, but in my case the answer was in the test description:
“TEST 8 response returned by receiveMessage contains name of message” I was building receiveMessage without actually returning anything.
Not sure if that will help, but I spent at least 3 hours before realizing what I was missing. If that doesn’t help, the other things that helped me was looking at the output example, and looking in studentgrade.spec for clues. Also reading and re-reading this whole message thread. We’re definitely not the only ones who got stuck on this part. I was also feeling similarly, “Sounds so simple! Why can’t I get it?”

Just hoping on to say this majorly brought out imposter syndrome for me too. I definitely was questioning if I could do this.

Did you put message: message.name anywhere in the receiveMessage function? And also pass it in as a parameter to the function. That’s what I did in order to call it later.