Record Collection Challenge help!

Tell us what’s happening:
Describe your issue in detail here.

trying to solve the second problem

  • If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.

and here is my current code for it
else if (prop === “tracks” && !records[id].hasOwnProperty(“tracks”))

can anyone give me any hints or suggestions in the right direction? thanks

Your code so far


// Setup
var recordCollection = {
 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(records, 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 (prop !== "tracks" && value !== "") {
   records[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 (prop === "tracks" && !records[id].hasOwnProperty("tracks")) {
   
   
//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 (prop === "tracks" && value !== "") {
   records[id][prop].push(value);

//If value is an empty string, delete the given prop property from the album.
 } else if (value === "") {
   delete records[id][prop];
 } 
 return records;
}

updateRecords(recordCollection, 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/92.0.4515.159 Safari/537.36

Challenge: Record Collection

Link to the challenge:

Your else if statement looks good. Now add some code to the block.


Here’s an empty array

[]

Here’s how you assign an empty array to a variable

someVariable = [];

Here’s how you add a value to an array

const someArray = [];
const someValue = "foo";
someArray.push(someValue); // someArray is ["foo"]

solved it!

else if (prop === “tracks” && !records[id].hasOwnProperty(“tracks”)) {

records[id][prop] = [];

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

I think what threw me off was that it said to create an empty array and add value to it.
If I just create an empty array and push value to it, it wouldn’t really make sense so
I made an empty array with the properties records[id][prop] and pushed value to it.

Can anyone comment if my thought process was correct here?

1 Like

Yep, that is what you’re intended to do. Good work figuring it out!

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