Record Collection Pointers

Hi all,

I’ve been working through the basic javascript curriculum and have been stuck for some time on Record Collection. Initially, my code was failing on:-

Initial Fail (now solved):

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

However, I was able to figure this out and pass this test the consequence to this is that I’m now failing the following test which was passing before:-

Current Fail (6/7 Passed):

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

After trying to debug the code I believe the reason for this regression is connected to the .push command and that it’s overwriting the previous if statement.

My Code:

// 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"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {

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

else if ( prop === "tracks" &&  value !==""){
let tempArray = [];
tempArray.push(value);
collection[id][prop] = tempArray;
} 

else if ( value === "") {
delete collection[id][prop];
}

return collection;

}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Any pointers are greatly appreciated :sunglasses:

For this challenge your logic needs to include checking wether collection[id].tracks is already an array of tracks or not.

1 Like

Thanks for the tip, although my solution feels “clunky” I was finally able to get all the tests to pass :+1:

function updateRecords(id, prop, value) {

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

else if (prop === "tracks" && collection[id].hasOwnProperty("tracks") === false) {
let tempArray = [];
tempArray.push(value);
collection[id][prop] = tempArray;
}

else if (prop === "tracks" && value !=="") {
collection[id][prop].push(value);
}

else if (value === "") {
delete collection[id][prop];
}

return collection;

}

Congratulations on figuring it out! Happy coding.

1 Like