Tell us what’s happening:
Its been 28 days since I last posted about this problem. In that time I have worked through nine chapters of Udemy’s JS Boot Camp course, as well as taking a dive into algorithms and flow charts. Its been amazing… and I still cannot solve one of the requirements for this challenge!
Good news is I have solved three of the four requirements: its the " If prop
is tracks
and value
isn’t an empty string, add value
to the end of the album’s existing tracks
array, " that’s throwing me off. I feel like I am so close though!
I have attempted a number of variation of .push(), but most have resulted in “‘x’ is not a function” typeError. Here are those attempts:
prop = 'tracks'.push(value); //not a function
records[id][prop].push(value); //not a function
value = records[id][prop].push(value); //not a function
records[id]['tracks'].push(value); //not a function
records[id][prop][value].push(); //undefined
value = records[id][prop][value].push(); //undefined
I have now combined the two requirements for populating the ‘tracks’ property and creating the ‘tracks’ property if it does not exist. That currently looks like this:
for (let k of Object.values(recordCollection)) {
'tracks' ? records[id][prop].push(value) : records[id][prop] = value;
}
…and it has given me some interesting results:
If --updateRecords(recordCollection, 2468, “tracks”, “Free”)-- is run in console, it updates the track to the end of the array, but three times.
If --updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’)-- is run, it throws that same old error message “‘x’ is not a function”.
I included in that screen the fact that I understand how to add a value to the ‘tracks’ property using the actual names (recordCollection[1245][‘tracks’].push(‘Random Track’)), but after hours of searching, I cannot find a reference to using function variables to add value to an Array whose parent is an Object nested inside other Objects. I did run a console.log(k) on that ‘for…of’ statement, which clearly shows that it iterates through the key:value pairs of the Object. But…??
I’ve come a long way, but am running out of ideas. What I want most is to be pointed to a resource where I can study and learn the syntax for this particular ‘Object nesting Arrays.push()’ situation. Any ideas?
Thank you!
// Setup
var recordCollection = {
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(records, id, prop, value) {
if(prop !== 'tracks' && value.length !== 0) {
records[id][prop] = value;
}
else if (value.length === 0) {
delete records[id][prop];
}
for (let k of Object.values(recordCollection)) {
'tracks' ? records[id][prop].push(value) : records[id][prop] = value;
}
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0
Challenge: Record Collection
Link to the challenge: