Record Collection/Brain Fire

Tell us what’s happening:
Hi!
So, I have written a bunch of code here, that when I “read” it, in relation to the psuedo-code in the instructions, seems to make sense --when weighed against the information and tutorials I have been finding online. But obviously something is not right.

Not sure where to go from here… Any suggestions?

  **Your code so far**

access nested key values in object literal// 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 !== "") {
  records[id][prop] = value;
}
else if(prop === "tracks") {
  if (records[id].hasOwnProperty("tracks") === null) {
    records[id].prop = [];
    records[id][prop].push("value");
  }
}
else if(prop === "tracks" && value !== "") {
  records[id][prop].push("value")
}
else if(value === "") {
  records[id].pop("prop");
}
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:

Can this second condition ever run?

pop only works on arrays. I think you want to look up delete.

1 Like

you have an issue here too, what are the possible outputs of hasOwnProperty?

1 Like

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:

Why not just check if the value is an empty string?

Why a for? You only need to change a single record.

tracks is truthy, only the first statement in the ternary will execute

Jeremy – I originally was checking for empty string with:
value !== -1
But I could not get that to work. My research led me to the .length as a potential solution, and that change caused the code to start functioning. What am I missing on that note?

About the for...loop: I was on a Stack Overflow thread called " Add property to an array of objects. One of the high-reputation commenters said, “Loop through the array. Add properties to each array element while looping.” I had already tried all of the avenues I shared above – records[id][prop].push(value), etc – with no positive results, and I imagined looping through all of the property values was a more universally useful tool as justification. Are you saying that is the wrong direction to explore? Is some variation of the records[id][prop].push(value) syntax the final solution? If so, sure would appreciate a point in the direction of some documentation I can wrap my head around.

Ilenia – Great clue… So by making ‘tracks’ falsey, !'tracks', I can force the second statement to be considered?

Thanks to you both for your responses,
Seven

Why not just check if value === ""? You always want the action of the code to be as obvious as possible.

Yeah, you don’t need to loop over the entire object. You should only update one thing.

Honestly, the hardest part of this challenge is understanding what every variable is. records is an object holding a bunch of record objects. records[id] is a specific record in the collection. records[id][prop] is a specific property of that individual record, like the artist or the tracks on the record.

In words, this part is asking you to add the value to the array if you’re updating the tracks, with the note that you may need to create the array if it does not already exist.

in that way only the second statement works, at this point just remove the ternary and leave the statement you want executed

Tell us what’s happening:
Okay, so I have discovered various nuances over the last couple of days that have led me to an even closer version of a working Function. And thank you to @ JeremyLT and @ ilenia for your hints…

As it stands now, the parts of the function work separately, but throw error codes in funny places. Specifically, the test updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me") receives a records[id].tracks.push is not a function error. The funny part is that the part of the code that handles that test is before the part with .push() code. So I am not sure why it is even looking there, i.e., since “tracks” does not exist inside the “5439” object, and the earlier “if” statement handles the placing of the new object “tracks” just fine (when the .push() portion is commented out), why is it scoping inside the next “if” statement where .push() is contained? Especially since when running the test updateRecords(recordCollection, 2468, "tracks", "Free"), the track “Free” is pushed to the end of the proper Array just fine??

Any assistance will be appreciated. :slight_smile:

   **Your code so far**

// 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 !== '') {
 records[id][prop] = value;
}

if(prop === 'tracks' && !records[id].hasOwnProperty('tracks')) {
 records[id][prop] = value;
}

if(records[id].hasOwnProperty('tracks')) {
 records[id]['tracks'].push(value);
}

if (value === '') {
 delete records[id][prop];
}

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:

both of these if statements can (and will!) run if the property is "tracks". I would look at using else if statements, because you are not fully separating your cases by only using a series of if statements.

Also, records[id]["tracks"] must be an array. Is this an array:

1 Like

@JeremyLT , would you be willing to say more about why " records[id]["tracks"] must be an array?" I feel like there is some key to my understanding the proper syntax for accessing these nested objects/arrays.

it’s written in the requirements

Okay, copy that, I mis-read @JeremyLT reply to mean that writing records[id]["tracks"] in that syntax somehow told the computer that it is an Array… because that has been the biggest struggle with this challenge: finding the proper documentation/example that outlines the syntax for accessing these deep nested objects/arrays.

Since @JeremyLT compared records[id]["tracks"] to records[id][prop], I thought he was making the point that the former created and array and the latter didn’t. Is that true?

what makes it an array or not is what you assign to it, not the syntax used to access the property

1 Like

Sorry, yeah, just not getting that.
Any examples available?

To put in other words, when you modify the "tracks" property, the result should be an array.

This line does not create an array, so it cannot be correct.

it should be an array but is not an array because the right hand side is not an array

it is not the syntax on the left that says if it’s an array or not, it is what you assign to it on the right hand side of the =

1 Like

Okay, finally grok that… and also finally passed the test. I have a lot to learn still…

Appreciating the support…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.