Record Collection Challenge I'm Stuck! [Solved]

I’m currently stuck on this challenge:

You are given a JSON object representing a part of your musical album collection. Each album has several properties and a unique id number as its key. Not all albums have complete information.

Write a function which takes an album’s id (like 2548), a property prop (like “artist” or “tracks”), and a value (like “Addicted to Love”) to modify the data in this collection.

If prop isn’t “tracks” and value isn’t empty (""), update or set the value for that record album’s property.

Your function must always return the entire collection object.

There are several rules for handling incomplete data:

If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.

If prop is “tracks” and value isn’t empty (""), push the value onto the end of the album’s existing tracks array.

If value is empty (""), delete the given prop property from the album.

// 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

// Here is my code that is not working

function updateRecords(id, prop, value) {
    if (prop !== "tracks" && value !== ""){
    collection[id][prop] = value;
  }  
  
  else if (prop === "tracks" && collection[id][prop] === "") {
    collection[id][prop] = [];
    collection[id][prop].push(prop, value);
    }
  
  else if (prop === "tracks" && value !== "") {
    collection[id][prop] = value;
  }
  
  else if (value === "") {
    delete collection[id][prop];
  }
  
  return collection;
}



// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Any help would be much appreciated. Thanks!

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

Also, I haven’t done Record Collection for ages, and I think it’s changed! So I’ll have a crack at it and report back :slight_smile:

1 Like

I’ve dug into your code a little and I have some pointers for you.

Check out this screen shot, which shows your code with the second test case loaded into it and the right side of the screen shows the error console in Chrome Dev tools:

Ignoring the Google Analytics error, we can see you’re getting a type error:

Cannot read property 'push' of undefined...

While this might not give us the exact answer, it’s a clue.

Look at this section of your code again:

else if (prop === "tracks" && collection[id][prop] === "") {
    collection[id][prop] = [];
    collection[id][prop].push(prop, value);
    }

and try to explain to me in your own words what you think it is doing (and if that’s what you really want it to be doing). I think you’ll find the culprit might be in here…

Hi @JacksonBates

Thanks for taking an interest in my work

I want that portion of the code to add a “tracks” property to the object when this property doesn’t currently exist within the object and add an array for the corresponding values (i.e. tracks) within the object. So I’ve since modified that portion of the code but it still isn’t working. Here is the new code, which I will break down step by step.

else if (prop === "tracks" && collection[id][prop] === "") {
    collection[id][prop] = prop;
    collection[id][prop] = [];  
    collection[id][prop].push(value); 

For the first line:

else if (prop === "tracks" && collection[id][prop] === "")

If the prop plugged into the function is equal to “tracks” and the collection object does not have that property, then add the property:

collection[id][prop] = prop;

Create a new array:

  collection[id][prop] = [];  

And add the values to the array:

 collection[id][prop].push(value);

Thoughts?

I’d have to double check this to be sure, but collection[id][prop] === "" is not the same thing as “has no property called tracks”, it instead is checking “does this have a prop called tracks that is currently set to an empty string”.

@JacksonBates
Even if I change the “” to undefined it still doesn’t work. Any ideas on how to fix it?!

The way I tested that specific condition was like this:

Spoiler
if (collection[id].tracks) {
      collection[id][prop].push(value);
    } else {
      collection[id].tracks = [value];
    }

Each time you resubmit your code - double check which tests you are passing and failing.

Also keep one eye on the error console to see if it tells you anything else. You can add little console.log(…) statements in there to test your assumptions too.

I’ve gotta go make dinner - I’ll check in again later to see how you went :slight_smile:
Good luck

@JacksonBates

Deleting my previous code paragraph and replacing it with the above unfortunately isn’t the solution. Let me know if anything else comes to mind.

I tried redoing my code:

function updateRecords(id, prop, value) {
    if (prop !== "tracks" && value !== "") {
    collection[id][prop] = value;
  }  
      
  else if (prop === "tracks") {
    var check = collection[id].hasOwnProperty('tracks');
    if (check === false) {
      collection[id].tracks = [];
      collection[id][prop].push(value);
    }
    else if (check === true) {
      collection[id][prop].push(value);
    }
    
  }
  
  else if (value === "") {
    delete collection[id][prop];
  }
  
  return collection;
}

The only error message I’m getting after trying this code is:

After updateRecords(2548, “tracks”, “”), tracks should not be set

Help?

Re-paste your current code and which tests are failing.

My little block above wouldn’t work perfectly as a replacement for the one you removed, because I have that embedded in a slightly different block. I was just showing that as an example of one way to check for a missing track property.

The structure of mine was like this (in English):

If the prop is not tracks and the value is not blank, set the prop of the ID to the new value. If the prop is tracks and the value is not blank then check whether that ID has a track property - if it does, add the new track value to the existing array, otherwise create a track property and set the value. If none of the above is true and the value is empty, delete the prop.

You’re pretty close to the solution.

Oh, I just saw your update. Gimme a minute to read it :slight_smile:

SO CLOSE :smiley:

Check this line

else if (prop === "tracks" ) {

For the final test, the prop is tracks, so this is the condition that gets hit. This means your if block never reaches the delete bit at the end. Think about how you can make the condition above a little more specific so that you don’t get that false positive for the last test.

Woohoo! I changed that line to this:

else if (prop == "tracks" && value !=="") {

Worked like a charm. Dang that challenge was tough! Thanks for the suggestions @JacksonBates

Anytime. Record collection is perhaps the hardest early JS challenge from what I remember - If you can persevere and solve that one, you can do them all. :slight_smile:

Who can understand all of this code especially if you’re a beginner? I’m just learning Javascript and I’m lost as far as this record collection is concerned. Is there anyone in the same boat as me?

4 Likes

Try going overboard with comments, that’s how I finally got past it. Just jot down what you are trying to do with each section. Use the requirements of the challenge to help plan the logic of the solution.
Sometimes I would just press run to see the errors, that was sometimes a help.
Also when testing, feel free to use console.log to show you the current contents of variables, counters, etc.

I didnt look at any of the answers above… i really want to get this on my own… Can you recommend any challenges before this one that i can go look at to refresh my memory? nvm got it… duh.

@JacksonBates @rforster thanks for posting this. :slight_smile: Your discussion helped me in understanding how this whole thing works.
I am fairly new to the JavaScript code and this challenge indeed was tough for me.

Thanks :slight_smile:

2 Likes

so many brain farts!!!

I am not quite understanding how you derived this. I am currently struggling with with exercise. I feel like I do not completely understand the logic needed to get to the solution and it is frustrating me as i feel that I am not going to achieve my goal in good time.

How can one improve on getting the logic right. Or rather, learning to choose the correct functions and expressions to get to the correct outcome.

Thank you