Challenge 5 Megathread

This is why we define functions.

Regardless of what the user chooses as the object or the job, our function, assuming the inputs comply with the function’s requirements, will process those parameters. The output is dependent of the user input, but the processing of those parameters should not be.

1 Like

I think the phrase here is “Garbage In, Garbage Out”? :stuck_out_tongue:

If Console.log() is not enough to see what’s going on, try to run your code using the example input on these web applications:

https://replit.com/languages/nodejs

or

https://pythontutor.com/visualize.html#mode=edit

2 Likes

I didn’t overthink this one and remembered to look back at what I was reading yesterday for how to add a property to an object. Works great!

1 Like

So no need to set firs name, etc to variables, you can already access them through the object itself. It looks like you are hard coding “Chris”…etc but there is no need for that since the function will pass in a astronaut object with those fields.

The goal to to add a job property to astronaut, and it is as simple as assignming a value like we did in previous challanes. then just return the astronaut!

1 Like

Just a hint, you don’t need to enter all of the name, nickname, prefix again because that information is passed into the function (behind the scenes) when it calls the function.

For the new job, it may not be “shuttle DJ” if it passes a different astronaut into the function so it’s better to have it point to the parameter instead of a specific value for that parameter (e.g. “Shuttle DJ”.

just remember if you want a parameter from an object → object.parameter, you can also use that for assigning new values to it.

3 Likes

One can modify the value of an existing object’s property, or add a new property to the object, without having to interact with the other properties of this object.

You are my hero!! I figured out the first line myself and corrected the return line after reading your comment.

I’m glad I was able to assist. The merit is all yours, though.

1 Like

Hey everyone! There are multiple objects so hardcoding DJ won’t work. The top of the function brings in the specific astronaut object as well as a separate specific job variable. You just need to append the variable to the object and Voila!
Hope you don’t have any issues!

1 Like

Never thought I would keep up the momentum. Finished day 5 challenge with two lines of code. :slight_smile: I’m usually very lazy and never go past day one of any challenge. but this has been different. good luck to all of you.

7 Likes

Hi, when I put console.log(astronaut); inside my working function, addJobToAstronaut, this is what I get for running test #2:

Running test: The addJobToAstronaut function should return the complete astronaut object

{“firstName”:“Chris”,“lastName”:“Hadfield”,“nickname”:“Space Oddity”,“prefix”:“Astronaut”,“job”:“Shuttle DJ”}

{“firstName”:“Chris”,“lastName”:“Hadfield”,“nickname”:“Space Oddity”,“prefix”:“Astronaut”,“job”:“Shuttle DJ”}

{“firstName”:“Chris”,“lastName”:“Hadfield”,“nickname”:“Space Oddity”,“prefix”:“Astronaut”,“job”:“Shuttle DJ”}

{“firstName”:“Chris”,“lastName”:“Hadfield”,“nickname”:“Space Oddity”,“prefix”:“Astronaut”,“job”:“Shuttle DJ”}

{“firstName”:“Chris”,“lastName”:“Hadfield”,“nickname”:“Space Oddity”,“prefix”:“Astronaut”,“job”:“Shuttle DJ”}

{“firstName”:“Julie”,“lastName”:“Payette”,“nickname”:“GG”,“prefix”:“Astronaut”,“job”:“Instructor”}

{“firstName”:“Julie”,“lastName”:“Payette”,“nickname”:“GG”,“prefix”:“Astronaut”,“job”:“Instructor”}

{“firstName”:“Julie”,“lastName”:“Payette”,“nickname”:“GG”,“prefix”:“Astronaut”,“job”:“Instructor”}

{“firstName”:“Julie”,“lastName”:“Payette”,“nickname”:“GG”,“prefix”:“Astronaut”,“job”:“Instructor”}

{“firstName”:“Julie”,“lastName”:“Payette”,“nickname”:“GG”,“prefix”:“Astronaut”,“job”:“Instructor”}

It seems like two profiles are being tested, however, when I crash the code it only shows one profile as an example of why it failed. Is there a way to know if other profile have passed the second test or not?

Two profiles exist, so you have to use the variable instead of hard coding the job.

1 Like

From all these challenges most of the code has been done already, so far there’s only a line or 2 of code needed to really get the result you need. I know it can be overwhelming trying to understand what’s going on (I struggled the first few days with it) but I’m learning to relax and stop over thinking, as said by someone previous, step away for a few minutes and let yourself come back later to it so you don’t stress out over it. Read the instruction and try and pick out exactly what they need.

Hint, look at the first part of code given to you, it gives you two objects… one which has properties that are listed on the left hand side under the “instructions”

2 Likes

I passed the challenge, but want to do a little extra. Don’t know why my console.log won’t output anything on the code below:

one=addJobToAstronaut(exampleAstronaut, “Shuttle DJ”);
console.log(one.firstname+": is a " + one.job);

Hi, a complete newbie here.
Since someone said it was a “2 line job”, I figured it’d be something like this below, but I need help. It says I have 2 failing tests.
Again I have no idea what I am doing here, so apologies for being useless…

addJobToAstronaut.job = new job(‘Shuttle DJ’);
return astronaut.job;

Am I somewhat close??? Thanks for the help.

1 Like

if you attach a string, then everyone becomes a “shuttle DJ”, no? shouldn’t you be attaching a variable instead… that’s what i did and it seemed to have worked…

1 Like

Hi @erikalamode , it is a one line solution. if you refer to the hints/tutorials it will direct you to MDN where you should review Objects. You will need to know how to access objects and how to add a new key: value pair to an object. another hint would be to look into the spread operator. Remember you want to return the original object with a new key: value pair added to it. Also look at your return statement. what is that actually returning… when you access astronaut.job that evaluates to the value stored in the astronaut object at the key of job.

Check out the help doc posted in the hints section - Working with objects - JavaScript | MDN

Astronaut is the object and the whole object is being passed into the addJobToAstronaut function here:
addJobToAstronaut = (astronaut, job)

So you have the object astronaut and the job variable. The job variable will hold whatever job the astronaut has. It can hold any string value. You are assigning the string that the job variable is currently holding to your new job property. There is more than one way to assign a new property to an object and that article goes over it. Hope those little hints are helpful

Yes, you are correct. I did not want to give away all of it. What is passed on to the object.property is the value of the new property, as passed on by the function.

2 Likes