Record collection object[id][prop].push()

Tell us what’s happening:
Passing the
After updateRecords(collection, 5439, "artist", "ABBA") , artist should be ABBA
After updateRecords(collection, 2548, "artist", "") , artist should not be set
After updateRecords(collection, 2548, "tracks", "") , tracks should not be set
After updateRecords(collection, 1245, "albumTitle", "Riptide") , albumTitle should be Riptide

these dosen’t pass
After updateRecords(collection, 5439, "tracks", "Take a Chance on Me") , tracks should have Take a Chance on Me as the last element.

After updateRecords(collection, 1245, "tracks", "Addicted to Love") , tracks should have Addicted to Love as the last element.

After updateRecords(collection, 2468, "tracks", "Free") , tracks should have 1999 as the first element.

I guess it can’t be the object[id][prop].push(value) ; / object[id][prop].push([value])
because in either case they should be placed last /first. Based on if there is an array already. So I guess it must be either of the if statements.

Your code so far


// Setup
var collection = {
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(object, id, prop, value) {
// If prop isn't tracks and value isn't an empty string, update or set that album's prop to value.
if (object[id][prop] !== "tracks" && value !== "") {
  object[id][prop] = value;
}
// If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it.
else if (object[id][prop] === "tracks" && !object[id].hasOwnProperty()){
  object[id][prop].tracks.push([value]);
}
// If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array.
else if (object[id][prop] === "tracks" && value !== ""){
  object[id][prop].push(value);
}
// If value is an empty string, delete the given prop property from the album.
else if ( value === "") {
  delete object[id][prop];
}
  

return object;
}

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

Challenge: Record Collection

Link to the challenge:

object[id][prop] !== "tracks"

this is referring to the object, you have to use instead the argument prop that is passed to the function

!object[id].hasOwnProperty()

it doesnt have what property? surely you mean to say if it doesnt have prop or ‘tracks’.

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

this will fail. you add an array. if it does not exist first make empty array then add the element

else if (object[id][prop] === "tracks" && value !== ""

refer to my first comment. you are referring to the object. would it be clearer to say something like “if the argument passed to the function is tracks and the value argument passed also is not an empty string”

Thanks for your help :slight_smile: . just wondering is there an easier way to know if you have to refer to the object or the argument in the function?. I thought I had to refer to the object and then specific the id and prop so it would know which one I meant.

1 Like

The challenge code descriptions and pseudo code can at times be confusing. Personally, i look at the resulting test cases. I see what is failing and what is passing and I compare that to the object I am returning.

In the case of this challenge in your conditionals youd be checking the arguments passed (you also have to check if the object has tracks prop that is how you would know whether to create an empty array and add a tracks prop if it didn’t exist) , but performing an operation on the object.

1 Like