I'm confused about how to do the "Manipulating Complex Objects" exercise and there's no hint page

I'm confused about how to do the "Manipulating Complex Objects" exercise and there's no hint page
0

#1

I’m confused about how to do this exercise and there’s no hint page:

I don’t understand how you enter another album into the existing data structure without overwriting the data that’s already there. It says “If you want to add more album records, you can do this by adding records to the top level array,” but it’s not clear how you do that.


#2

It helps to think about what type of data structure you are dealing with and how you usually add to that…

What type of thing is the variable myMusic?


#3

There is no hint because any hint would give you the answer: I suspect you are overthinking this. Seconding what @JacksonBates says.

As an aside, I wish it didn’t mention JSON at all there, there is no need for it and I think it confuses things.


#4

This is what I put in but it won’t accept it:

var myMusic = [
{
“artist”: “Billy Joel”,
“title”: “Piano Man”,
“release_year”: 1973,
“formats”: [
“CS”,
“8T”,
“LP” ],
“gold”: true
}
// Add record here
{
“artist”: “Frank Zappa”,
“title”: “Hot Rats”,
“release_year”: 1972,
“formats”: [
“CS”,
“8T”,
“LP” ],
“gold”: false
}
];


#5

You are very close!!!

But again, what data type is the thing you are adding this second object to? How do you normally add a new thing to things of that data type?


#6

I notice they’ve updated the instructions on this challenge since I last looked. Read them very carefully, and the one thing you are missing is mentioned there…


#7

I looked over the instructions again, still lost. I understand that myMusic is an array, but it seems like the albums should be myMusic[1] and myMusic[2], and I can’t get that to work.


#8

I put this in:

var myMusic[1] = [
{
“artist”: “Billy Joel”,
“title”: “Piano Man”,
“release_year”: 1973,
“formats”: [
“CS”,
“8T”,
“LP” ],
“gold”: true
}
];
// Add record here

var myMusic[2] = [
{
“artist”: “Frank Zappa”,
“title”: “Hot Rats”,
“release_year”: 1972,
“formats”: [
“CS”,
“8T”,
“LP” ],
“gold”: false
}
];

and I got the error message “myMusic[1] should be an object,” which confuses me. Why isn’t it an object?


#9

I found a solution here that worked: https://github.com/FreeCodeCamp/FreeCodeCamp/issues/9858


#10

Your intuition is sensible, but javascript doesn’t quite work the way you are thinking.

Presumably you want myMusic[1] and myMusic[2] to be the first and second item in the myMusic array?

Firstly, computers start counting at zero, so the reference in the error message to myMusic[1] is actually the test runner complaining that you haven’t set an object as the second item in the array. This is true, you’ve tried to set it as an array that contains an object:

[{.....}] instead of just {.....}

However, setting each item in the array the way you have doesn’t quite work like that in javascript either.

To simplify, you have an array with one item, eg, [0]

To add to that array, add a comma and the new item eg [0, 1]

The same principle is used whether you are adding strings, numbers or objects - use a comma to separate them.


#11

ie, like [item1, item2], not [item1 item2]


#12

Thanks, I had forgotten about the counts starting at 0.


#13

Yes, that’s the issue.

MANY people have missed the comma in the past - read through this and see how many people tore their hair out over the comma…it’s reassuring to know you weren’t the only one :slight_smile: :


#14

P.s this is my first FCC contribution and I have been coding for one month now,

Spoiler alert below this explanation!!!

There is a very discreet but VERY important comma missing at the end the first myMusic element,

This is a simple comma after the closing brace for the first element. By adding in this comma, the computer then recognises that this element is closed, and you can open a new element within the same Array.

var myMusic = [
{
“artist”: “Billy Joel”,
“title”: “Piano Man”,
“release_year”: 1973,
“formats”: [
“CS”,
“8T”,
“LP” ],
“gold”: true
},
// Add record here
{ “artist”: ‘Pink Floyd’,
“title”: “Animals”,
“release_year”: 1977,
“formats”: [
“CD”,
“CS”,
“8T”,
“LP”],
“gold”: true
}
];


#15

I read this reply and it really helped. Thank you.


#16

Technically, the comma can be the very first character before { and it will pass with a warning.


#17

The comma got me!!! Thank you!


#18

I pushed a new record to the existing variable:

myMusic.push(
  {
  // properties here
  }
);

#19

Eventually this finally got through. Thank you sir!