Challenge "Basic JS: Record Collection" Bug

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');

2

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:

the tracks property should always be an array, in your second screenshot for 5439 it is not an array

1 Like

Aw I didn’t even notice.

I assumed the line before object[id][prop] = []; would create an empty array then would be filled with the song by the line object[id][prop] = value;

Not quite. Those two lines first make the tracks hold an empty array, then assigns a single value not in an array to the tracks instead.