Record Collection challenge - need help

Tell us what’s happening:
Can someone tell me why I am getting this error and how I can fix it?

  **Your code so far**

// Setup
const recordCollection = {
2548: {
  albumTitle: 'Slippery When Wet',
  artist: 'Bon Jovi',
  tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
  albumTitle: '1999',
  artist: 'Prince',
  tracks: ['1999', 'Little Red Corvette']
},
1245: {
  artist: 'Robert Palmer',
  tracks: []
},
5439: {
  albumTitle: 'ABBA Gold'
}
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop != 'tracks' && value != "")
{
  records[id][prop][value] = value;
}

if (prop == 'tracks' && records[id].hasOwnProperty('tracks') == false)
{
  let records[id][prop] = [];
  records[id][prop].push(value);
}

if (prop == 'tracks' && value != "")
{
  records[prop].push(value);
}

if (value == "")
{
  delete records[id][prop];
}

return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0

Challenge: Record Collection

Link to the challenge:

1 Like

HI @lepros !

The reason why you are getting an error is because it looks like you are trying to create a variable called let records[id][prop] and the computer doesn’t know what to do with that information.

Here’s a hint
This part of the challenge

only needs to be one line of code.
Not two.

You need to refactor that first line because the directions say to create an empty array and add value to it.

Hopefully that helps!

1 Like

The instruction then is confusing. “…create an empty array and add value to it.” sounds like two steps. Either way, it is confusing. If I add value to it then I’m not creating an empty array am I? That’s just my perception.

Anyway, I change it to one line.

1 Like

You don’t need to create an empty array with let here. let is used create new variables.
record[id] object already exists. So, we are not creating any new thing here. We just need to add an empty array to its ‘tracks’ property. So remove the let.
Two lines of code are needed - one to create a new array and another to push element to it, which is correct.

Also usually a strict type checking is recommended in conditions but not mandatory.

1 Like

you can use push and that is totally fine, but there is another solution where you can just write it with one line by just placing value into an array.

Both options are fine though :slight_smile:

1 Like

Ok, now I just have this:

records[id][prop].push(value);

image

1 Like

Your current one line solution is not going to work because records[id][prop] doesn’t have a tracks property here according to this condition you wrote.

if (prop == 'tracks' && records[id].hasOwnProperty('tracks') == false)

So you are trying to push to nothing essentially.

You were super close with this line here

Now that you have removed let, the last step is to add value to the array.
Don’t worry about messing with the push method for this one line solution.

I promise it is easier then we are trying to make it.
Hopefully that is clearer.

1 Like

I guess another way would be to say create an array with value in it :woman_shrugging:

1 Like

records[id][prop] = [value];

image

Can I see your full code?

// Setup
const recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop != 'tracks' && value != "")
  {
    records[id][prop][value] = value;
  }

if (prop == 'tracks' && records[id].hasOwnProperty('tracks') == false)
  {
    records[id][prop].push(value);
  }

if (prop == 'tracks' && value != "")
  {
    records[id][prop] = [value];
  }

if (value == "")
  {
    delete records[id][prop];
  }

  return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

hmmm… what does this line do?

Side note: don’t use ==, it can occasionally surprise you with coercision. Use ===

1 Like

Ahh…
I was under the impression you were working on the second condition here based on your initial post.

For the third condition you will use the push method.

It looks like you essential swapped these two.

1 Like

Also, your if statements aren’t mutually exclusive, so this won’t quite do what you expect.

2 Likes

I think it would help to break down each condition and get one working at a time.
That will help you see the logic better and understand the full challenge.

You are like 80% there, but you are not sure how each of these rules are supposed to work.

I would suggest pausing and just focusing on one at a time and get that one to pass before moving onto the next one.

1 Like

Am I supposed to just add “else” before the 2nd, 3rd, and 4th “if”? I tend to write these if statements like this because it’s how I read the instructions and so I just write it that way.

1 Like

Focus first on a couple of the things pointed out above

  1. This syntax is wrong (why?)
  1. You seem to have flipped the two cases for the tracks property
1 Like

I’ll come back to this later. I can’t deal with this anymore.

1 Like

Totally legit strategy. Breaks are good for your brain!

1 Like

I totally get it.
Record collection is hard when you first go through it.
I remember struggling with it when I went through the js curriculum.

As I mentioned earlier, you are closer to solving it then you think.
I just think breaking down the problem into smaller bite size pieces will help.

Take a break and revisit it later when you are refreshed. :grinning:

2 Likes