Build a Record Collection - Build a Record Collection


function updateRecords(records, id, prop, value){
  if(value=="") {
      delete records[id][prop]
  } else if (prop!=="tracks" && value!=="") {
      records[id][prop]=value
  } else if (prop=="tracks" && value!=="") {
      records[id].tracks=value
  } else if (prop=="tracks" && value!=="" && records[id].hasOwnProperty("tracks")==false) {
      let newProp=[value]
      records[id][prop]=newProp
  }
  return records

this fails every test but 1. I dont understand

hmm, what exactly is this checking for?

Edit - nope, I missed the last bit. That’s not the issue I think

Its this pair of if conditions that are the problem. You never run the last condition.

ok I have set the condition of the if property to check if that is has it, and I passed everything but 5 and 6 .

5. After updateRecords(recordCollection, 1245, "tracks", "Addicted to Love"), tracks should have the string Addicted to Love as the last element.
6. After updateRecords(recordCollection, 2468, "tracks", "Free"), tracks should have the string 1999 as the first element.

I’m not sure what that looks like. Can you please post the updated code?

function updateRecords(records, id, prop, value){
  if(value=="") {
      delete records[id][prop]
  } else if (prop!=="tracks" && value!=="") {
      records[id][prop]=value
  } else if (prop=="tracks" && value!=="" && records[id].hasOwnProperty("tracks")==true) {
      records[id].tracks=value
  } else if (prop=="tracks" && value!=="" && records[id].hasOwnProperty("tracks")==false) {
      let newProp=[value]
      records[id][prop]=newProp
  }
  return records
}

ok, what (in words) are you supposed to do if the tracks array already exists?

add the value to the existing “tracks” property

Yup. Are you adding the value to the array that already is there?

I thought so but I assume not, I tried using brackets on “tracks” like before with “prop”, but it makes everything fail.

wait, this worked for me:

 else if (prop=="tracks" && value!=="" && records[id].hasOwnProperty("tracks")==true) {
      records[id][prop].tracks=value

That is not a valid solution though.

You’ve written the code before, how do you add a value to the end of an array?

this is the only test left

5. After updateRecords(recordCollection, 1245, "tracks", "Addicted to Love"), tracks should have the string Addicted to Love as the last element.

its not? what is wrong with it?

What’s wrong is that you are not adding to the end of the array that already exists. You are destroying the array that already exists and replacing it with value.

oh. I didn’t even know I could do that. I figured you would have to delete it like I did in the first if statement and make another. Ig that’s a cool trick to keep in mind, but how do I fix this? I see this in my code, which I think adds a value to a property, but when I enter this format for the other statement, it fails.:

records[id][prop]=value

the code I posted before is the closest I got to passing, any other way returned test 6 as failed and some options make the whole thing fail

That right there destroys what ever was previously stored in records[id][prop] and completely replaces it.

Again, you need to add to the end of an array. You have already written a command once to do this. If you forgot what you wrote, I’d look back through this tread or ask Google “JS how to add element to array”.

Don’t focus on the tests. Focus on getting the logic correct.

I will sit with this for a little while, I really want to figure this out beyond this project, Im struggling with the concept. Maybe researching will help clarify the logic

I see now that prop is already representing tracks, so there’s no need for the .tracks. what I want to do is add to the array , so I need the array adding methods like un/shift and pop/push. I have passed, thank you so much

1 Like

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