Tell us what’s happening:
I dont even know what to ask, this problem came out of nowhere and has so many parameters I dont even know what to do. I even looked at the solution and I’m still puzzled. Can someone please lay this question out step by step so its easier to understand, and help guide me through it?
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 (prop !== 'tracks' && value !== '') {
collection[id][prop] = value;
}
if (prop === 'tracks' && value !== '') {
collection[id][prop].push[value];
}
if
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.
Your task is to write a function updateRecords which has 3 parameters, an id, a property, and a value: updateRecords(id, prop, value)
Within this function you write, you must satisfy all of the rules outlined. The general gist of the question is in the first condition:
If prop isn’t “tracks” and value isn’t empty (“”), update or set the value for that record album’s property.
So if prop isn’t equal to "tracks" and value isn’t "" then you must set collection[id][props] = value
That’s the question in a nutshell, a function that can update things in collection
The rest of the conditions are handling incomplete or missing data, and telling you what you should do in those cases (especially with tracks as it’s an array and not a single value. You should use conditionals like if to help you with this.
I get what you told me, but im still having problems, do you mind sharing your code so i can pick through it and figure it out. this is really bugging me. Heres my code if you want to take a look. I’m failing one test, which is: After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.
function updateRecords(id, prop, value) {
if (prop !== 'tracks' && value !== '') {
collection[id][prop] = value;
}
else if (prop === 'tracks' && value !== '') {
collection[id][prop].push(value);
}
else if (value !== '') {
collection[id][prop] = [value];
}
else delete collection[id][prop];
return collection;
}