Challenge 6 Megathread

Can anybody explain why my solution didnt work? My code was basically just returning this: arrayParam.push(objectParam).

For some reason all test cases were failing when I tried doing it that way. I had to take another approach to get things to work but I’m still confused why simply pushing the object into the array and returning it didnt work.

This is a general question. i have basic knowledge of JAVA, so this syntax is new to me. can someone help me understand why the function everyday has “const” in front instead of function? And now i see that to create an Array you need to start it with “const”? I assumed const was short for constant, like creating constant variables. but im thinking it has another meaning. can anyone explain briefly? thanks.

thats what i did and it worked… maybe you had a syntax error?

Very strange, I actually rewrote it multiple times to make sure I didnt have a syntax error. But it may be possible, I was working off of my phone.

Anyway thanks for replying I was worried I might be misunderstanding how the code actually works.

1 Like

you are correct that const is for constant variables. In JS you have options on how to declare a function… you can use function declaration like you are familiar with which allows for hoisting and is practical in classes and other situations. or you can use the function expression which assigns an anonymous function to a reference variable as the challenges show. inside of a function expression, you can use the function key word or the => syntax which has its own limitations.

1 Like

pushing or using the spread operator is a constant time complexity O(1) solution vrs a loop being O(n). there is no need to loop in this challenge.

1 Like

i just did “return roster.push(astronaut);” wondered why it didnt work for a little then seperated it to two lines and it was good to go. super easy.

3 Likes

The reason why “return roster.push(astronaut);” does not work is because the push method returns the length of the array.

3 Likes

Very interesting, thanks for this info. I can’t believe I never knew this.

I’m guessing if the push method just mutated the array but didn’t return anything the code still wouldn’t return the expected result if we tried to do it in one line as return roster.push(astronaut)?

For that to work the push method would need to actually return the new array right?

If you mutated the array using roster.push(astronaut);, you would just need to return roster; at the end.

If anyone is still struggling with this one, biggest hint:

Go to the hint link and look at section: Add an item to the end of an Array

so completed the challenge and wanted to take it a step further, is it possible to write the assignment in the return call? and if so how would some of you guys go about it?

Yes, it is possible to reach the required end state with one line of code.

You can check this article, click here

This is called the spread operator. And it creates a new copy of your object or array.

You could do all of the challenges(up to now) with one line indeed.

your code works just missing " ,roster);" at the end

I don’t understand

Failed: The addAstronautToRoster function should return an array with the astronaut inside
Reason: Your code ran but did not produce the correct result.
Hint: Make sure you are appending (pushing) the values inside the array!

Failed: The addAstronautToRoster function should return an array with the astronaut inside
Reason: Your code ran but did not produce the correct result.
Hint: Make sure you are appending (pushing) the values inside the array!

const addAstronautToRoster = (roster, astronaut) => {
// Code here!
const test = [“Chris”, “Hadfield”, “Space Oddity”, “Astronaut”];
return test;
};

What you are doing here is creating a new array for a specific astronaut object. All you have to do is add the existing astronaut object to the existing roster array.

1 Like

Not sure what I’m doing wrong. These two are the same but I’m failing one and passing the other?

Oh nvm I got it. My solution was incomplete

Hi there, I submitted challenge 6 at 9:01 pm PST, which I guess is past midnight eastern time…and I then I got an email that I missed the challenge. Are challenges required to be submitted before 9 pm PST/ 12 am EST?

1 Like

Very interesting, thank you for the explanation. I understand it better now.