Tell us what’s happening:
First of all, this is my first forum post and I’d like to say it would be hard to overstate the benefits of this project. Thank you to the administrators, so much.
For this challenge, I found a solution of my own but I was looking in the hint section to see how it was done, and I am not sure if the solution given is somewhat incomplete or if I am misunderstanding it.
In the given solution, if the property is “tracks”, and the album in question does not have a “tracks” property, the line of code executed is: “collection[id][prop] = value”.
But then if you were to send another value to this album’s tracks, it looks like it would try to push() it on, but I think it would be pushing a string onto a string, not onto an array. So I tried to see if you can turn a string into an array in JS by simply pushing another string to it, but it didn’t seem to work that way. Does it?
But maybe the solution in the hint section is only required to work for a single iteration of the function. That would make sense here.
Clarification would be much appreciated.
Your code so far
// 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 (value == "") {
delete collection[id][prop];
return collection;
}
switch (prop) {
case "tracks":
if (collection[id].tracks == undefined) {
collection[id].tracks = [""];
}
collection[id].tracks.push(value);
break;
case "album":
collection[id].album = value;
break;
case "artist":
collection[id].artist = value;
break;
default:
break;
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
console.log(collection[5439].artist)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection