Record Collection Challenge

I’m on the record collection challenge, but I’m having trouble with it. This is the function I coded, which doesn’t seem to be doing anything to the record collection:

function updateRecords(id, prop, value) {

if (id === true && prop === false) {
collection[id] = new Object(prop);
collection[id][prop] = value;
} else if (prop === true && value !== “”) {
collection[id][prop].push(value);
} else if (prop === true && value === “”) {
delete collection.id.prop;
} else {
return console.log(“invalid”);
}

return collection;

}


Here’s the instructions:

Write a function which takes an album’s id (like 2548), a property prop (like “artist” or “tracks”), and a value (like “Addicted to Love”) to modify the data in this collection. If prop isn’t “tracks” and value isn’t empty (""), update or set the value for that record album’s property. If value is empty (""), delete the given prop property from the album.

Can someone point out what I’m doing wrong?

Thanks!

  1. prop isn’t boolean (true or false) its a string
  2. Why are you creating an object?

Whoops…I did not realize that that was what I was doing. I was just trying to get the function to check whether the prop the user typed in (when using the function) existed or not. Do you know if there’s any way to do that?

1 & 2: I think I just got so confused…

But thanks for pointing out both. As for number 2, I thought I needed to create an object that nested itself within id, but now that you pointed it out, that’s not what the instructions call for…

@Shmi1

One of my practices to help understand instructions is to put each instruction on a separate line. Then, indent related instructions. Then, capitalize any action words.

IF prop IS "tracks"
 BUT the album doesn't have a "tracks" property,
     CREATE an empty array BEFORE
         ADDING the new value to the album's corresponding property.
 
IF prop IS "tracks" AND value ISN'T empty (""),
  PUSH the value onto the end of the album's existing tracks array.

IF value IS empty (""),
  DELETE the given prop property from the album.

Challenge provided hints

Use bracket notation when accessing object properties with variables.

Push is an array method you can read about on Mozilla Developer Network: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push.

You may refer back to Manipulating Complex Objects Introducing JavaScript Object Notation (JSON) for a refresher.

2 Likes