Record Collection (Help Question)

Here is the code so far after trying for a couple hours, I got my code to pass half the tests but I know that isn’t the point. I structured the statements in accordance to what it says in the challenge, after trying to figure out the problem I can say it’s probably the if statement with the .hasOwnProperty in it that may be causing problems? Is there 4 statements when there only needs to be 3? I don’t know, some help would be appreciated, Thanks!

Your code so far


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




// Only change code below this line
function updateRecords(id, prop, value) {
if (prop == "tracks" &&[collection][id].hasOwnProperty("tracks") == true) {
collection[id][prop] = [];
} else if (prop != "tracks" && value != ""){
 collection[id][prop] = value;
} else if (prop == "tracks" && value != "") {
 collection[id][prop].push(value);
} else if (value == "") {
 delete collection[id][prop];
}

 return collection;
}

updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

the issue is that in an if/else chain only one statement will execute

also the issue is that if the object has already the tracks property you are making it an empty array

and you have a syntax error

you have put collection in an array and try to access stuff from an array with only one property, ending with [collection][id] being undefined

Alright so I believe I corrected the syntax error and the fact I was making an existing tracks property into an array. I have tried to rearrange the order of the if/else statements so the others will execute but no luck. There is still one error, but all other tests are passing:

After updateRecords(5439, "tracks", "Take a Chance on Me") , tracks should have "Take a Chance on Me" as the last element.

// Setup

var collection = {

  2548: {

    album: "Slippery When Wet",

    artist: "Bon Jovi",

    tracks: [

      "Let It Rock",

      "You Give Love a Bad Name"

    ]

  },

  2468: {

    album: "1999",

    artist: "Prince",

    tracks: [

      "1999",

      "Little Red Corvette"

    ]

  },

  1245: {

    artist: "Robert Palmer",

    tracks: [ ]

  },

  5439: {

    album: "ABBA Gold"

  }

};

// Only change code below this line

function updateRecords(id, prop, value) {

if (prop == "tracks" && collection.hasOwnProperty("tracks") == true) {

  collection[id][prop] = [];

} else if (prop != "tracks" && value != ""){

  collection[id][prop] = value;

} else if (prop == "tracks" && value != "") {

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

} else if (value == "") {

  delete collection[id][prop];

}

  return collection;

}

updateRecords(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.

Please 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.

markdown_Forums

1 Like

only the first statement with a true condition in a chain will execute

if you do not have the tracks property you are just giving to the property a value of an empty array

There is only one statement which specifies the things I need to do if there is not a tracks property, right? This one:

If prop isn’t "tracks" and value isn’t empty ( "" ), update or set the value for that record album’s property.

but the array I have in my code isn’t in that statement it’s in in the rule that specifies this:

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.

I can’t seem to find where I put the empty array in my statement for that rule.

ah, sorry, it’s different, you are checking if collection itself has the property, which is always false, but if you was checking the right object, if you had already the property, you are overwriting it

then, as there is this error, any time you have the tracks property, just this:

in the case of the failed test you do not have the property, so you are trying to use push on something undefined

1 Like