I have completed all the exercises up to Record Collection and have the same problem. Neither the instructions nor the hint solutions make much sense. Is the update function assigning new names id, prop, and value to the number, title, artist and tracks, or should we replace the first with the second? Can anyone explain what is required in plain English, please?
It is pretty plain English already. But perhaps if you can just focus on asking one specific question at a time…
Here is the first paragraph of the instructions:
You are given an object literal representing a part of your musical album collection. Each album has a unique id number as its key and several other properties. Not all albums have complete information.
Is this part fully understood?
(The object literal has some information missing… just like say if you were to write a list of all your favourite songs. You may remember the name but not the singer. Or you may remember the singer but not all the songs etc)
Here it is:
You start with an updateRecords function that takes an object literal, records , containing the musical album collection, an id , a prop (like artist or tracks ), and a value .
The code “updateRecords” is a function we are told.
It accepts some inputs which are id , a prop (like artist or tracks ), and a value
So the code gets these from whomever is calling updateRecords (in this instance the test suite will be doing most of the calling but you can also call it in the same manner as the example given to you on the very last line… updateRecords(recordCollection,5439,’artist’,’ABBA’)
Yes, I can see the object literal. My initial question is whether to rename it as ‘records’ to match the question.
Then I need to understand how the later code recognises the ‘id’ number which is different for each record in the array.
Then whether id, prop, value need to be established as an array template or whether JS somehow recognises the switch items with prop, value or with albumName, artist and tracks.
Hope that’s not too confusing.
No. Do not rename the global variable. It is outside of the area you are supposed to edit, according to the comments. records is the function argument. You need this function to work for any record collection object, not just the single global object you were given.
What is the id? What data type? Can you console.log the function arguments?
I don’t understand this question. You have function arguments. The values of those arguments are determined when the function is called.
I have tried again with a more direct if/else if approach. At least it runs but still does not do what it is supposed to do for half the lines. Latest version:
// Only change code below this line
function updateRecords(records, id, prop, value) {
// if prop is not tracks and value is not "", leave alone
if (prop !== "tracks" && value !== "") {
records[id][prop] = value;
// if not above, if prop is "tracks" and
} else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
records[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
records[id][prop].push(value);
// if value is empty string, delete the prop from object
} else if (value === "") {
delete records[id][prop];
return records;
}
console.log(records, 5439,"artist", "ABBA");
}
I’m getting there, but … Thanks for all your help.
Did you mean indentation when you mentioned format?
Odd that copy-paste is deleting the spaces before this }
In any case, look at your {}s. You only return inside of the last if-else clase. I don’t think that is where you meant to put that.
Formatting helps catch mistakes that like that:
// Only change code below this line
function updateRecords(records, id, prop, value) {
// if prop is not tracks and value is not "", leave alone
if (prop !== "tracks" && value !== "") {
records[id][prop] = value;
// if not above, if prop is "tracks" and
} else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
records[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
records[id][prop].push(value);
// if value is empty string, delete the prop from object
} else if (value === "") {
delete records[id][prop];
return records;
}
console.log(records, 5439,"artist", "ABBA");
}