Record Collection JS challenge

Hello, without telling me the answer, does anyone have some good hints just to help me to put my thoughts together to solve the “Record Collection” challenge on Basic JavaScript?

What have you got so far? What parts are confusing you?

That’s what I have so far:

// Only change code below this line

function updateRecords(records, id, prop, value) {

  if (prop != "tracks" && value != "")  { 
    records[5439]['artist'] = "ABBA";
  }  else if (prop == "tracks")  {
      records[5439]["tracks"] = ["Take a Chance on Me"];

  }
    return records;

}

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

This won’t work - you don’t want to make code that only works for each test case.

Let’s step back to a simpler problem.

If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value.

Let’s do just this

update or set that album’s prop to value.

How can you access album id, property prop? How can you update album id, property prop to value?

1 Like

Something like that?

// Only change code below this line

function updateRecords(records, id, prop, value) {

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

}

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

Close, but why are you using ABBA?

// Only change code below this line

function updateRecords(records, id, prop, value) {
  if (prop != "tracks" && value != "") { 
     records[id][prop] = value;
  } else if (prop == "tracks") {
    records[id][prop] = [value];

  } 

  return records;

}

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

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Getting closer. Now you need to account for the fact that tracks is an array. If there is already a tracks property, then you need to add the value to the end of the array.

Ok , it’s that done by using the push() method?

function updateRecords(records, id, prop, value) {
  if (prop != "tracks" && value != "") {
  records[id][prop] = value;
} else if (prop == "tracks") {
  records[id][prop] = [value];
} else if (prop == "tracks" && value != "") {
  records[id][prop].push(value);
} else if (value == "") {
  delete records[id][prop];
}

return records;
}
  
updateRecords(recordCollection, 5439, 'artist','ABBA');

That doesn’t seems to be working yet.

Thanks a lot for your help.

You are close here. How do you check if an object has a property?

1 Like

By applying hasOwnProperty() method

Yeah, exactly! So the instructions have two cases. Case 1: record id does not have the "tracks" property. Case 2: record id does have the "tracks" property. You need to use hasOwnProperty() to differentiate these two cases.

records.hasOwnProperty(“tracks”)

?

But is it the records that has the "tracks" property? records[id][prop]

The id has the “tracks” property

Right, so records.hasOwnProperty("tracks") can’t ever be true. It needs a small modification.

records[id].hasOwnProperty(“tracks”)

?

What happens if you try it? What’s your code look like if you add that as a condition?

1 Like

that’s much better, yes

but better than this, why don’t you try it in your code?

2 Likes