Need help on Record Collection

Tell us what’s happening:

  **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 !== "tracks" && value !== ""){
  object[id][prop] = [value];
}
else if (prop === "tracks" && !object[id].getPropertyName(tracks)){
  object[id][prop] = [value];
}
else if (prop === "tracks" && value !== ""){
  object[id][prop].push(value);
}
else if (value === ""){
  delete object[id][prop];
}
return object;
}
updateRecords(collection, 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/88.0.4324.190 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

First let’s look at this part of your code:

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

[value] is different than value. In object[id][prop] you have to use brackets to use the variable, but in the second part you have to assign just the variable otherwise you would be changing the type, in this case the brackets means something else, it would be an array as you can test with the following. Try this:

if (prop !== "tracks" && value !== ""){
  object[id][prop] = [value];
 console.log(typeof(value));
 console.log(typeof([value]));
 console.log(value);//string
 console.log([value];//array
}

If the property isn’t “tracks” it could be albumTitle or artist, so it just needs the name of the album or artist, just the string not an array, so assign a string to it.

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

So it must be different from the next part. Tracks must be an array so you have value inside brackets: [value]. Which is correct in your code. Which leads us to problem 2: the second condition.

else if (prop === "tracks" && !object[id].getPropertyName(tracks)){//problem 2
  object[id][prop] = [value];
}

problem two:
so if prop is tracks but the property doesn’t exist you need to create an array with an element value inside. But to check if the property exists you have to rewind a few lessons before Record Collection, in the lesson named Testing objects for properties, which presents the .hasOwnProperty() method. The one you used I believe would be this one? MDN Object.getOwnPropertyNames().

So you would have this instead:

else if (prop === "tracks" && !object[id].hasOwnProperty('tracks')){
    object[id][prop] = [value];

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

Ok, I just started with answering, thanks. Will have that in mind.

1 Like

Ended up deleting that part

@CarolinaFreitas Thank you for the assistance, I just had a few logical errors in my code but thanks for pointing that out.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.