Hi,
In reference to the challenge here: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection
I worked through the challenge and have a working solution but the JSON object is not updated fully and as a result, will not pass the tests needed.
Specifically all tests pass beside the test as follows:
“After updateRecords(collection, 5439, “tracks”, “Take a Chance on Me”), tracks should have Take a Chance on Me as the last element.”
Running my code locally via CLI results in the JSON object being updated correctly, this is backed up by the CLI log of the returned object.
When printing the object in the freecodecamp console it shows that object not being updated even though it is the same code.
Code and Images below:
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.length !== 0){
console.log("in first if");
object[id][prop] = value;
}
else if(prop === "tracks" && !object[id].hasOwnProperty("tracks")){
console.log("in 2nd if");
object[id][prop] = [];
object[id][prop] = value;
}
else if(prop == "tracks" && value.length != 0){
console.log("in 3rd if");
object[id][prop].push(value);
}
else if(value.length == 0){
console.log("in last if");
delete object[id][prop];
}
console.log(object);
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/87.0.4280.88 Safari/537.36
.
Challenge: Record Collection
Link to the challenge: