Record Collection "MTayachi"

Tell us what’s happening:
I need some help with my script down below for this challenge, I don’t know why it’s not working, here’s the results of the test:
// running tests

After updateRecords(5439, “artist”, “ABBA”), artist should be “ABBA”

Cannot read property ‘pop’ of undefined

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

After updateRecords(1245, “tracks”, “Addicted to Love”), tracks should have “Addicted to Love” as the last element.

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

After updateRecords(1245, “album”, “Riptide”), album should be “Riptide”

// tests completed

Thank you.
Your code so far

js

// 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
function updateRecords(id, prop, value) {
  if (collection.hasOwnProperty(id) === false  && value ==! "") {
    if (prop ==! "tracks"){
    collection.id= {prop: value};
  } else {
    collection.id= {prop: []};
    collection.id.prop.push(value);
    }
  } else if (collection.hasOwnProperty(id) === true) {
        if (id.hasOwnProperty(prop) === false) {
          if (value ==! "") {
              if (prop ==! "tracks"){
            collection.id.prop= value;
              } else {
            collection.id.prop= [];
            collection.id.prop.push(value);
              }
              return collection;
          }
        } else {
          if (value === "") {
            if (prop ==! "tracks") {
            delete collection.id.prop;
            }
            else if (prop === "tracks" && collection.id.tracks === []) {
              delete collection.id.tracks;
            }
            return collection;
          } else {
            if (prop ==! "tracks"){
            collection.id.prop= value;
              } else {
            collection.id.prop.push(value);
              }
              return collection;
          }

        }
  }
  return collection;
}


// Alter values below to test your code
updateRecords(1245, "tracks", "ain't no sunshine");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection/

Seems to me you’ve kind of fallen down a rabbit hole. This one isn’t a complex as your code makes it out to be. I might suggest you step back, and look at what you’re trying to do. Write out the assignment in your own words, and work on some ‘pseudocode’, again in your own words. Here’s what I mean:

Possible Pseudocode Idea

I have to write a function to work on a array of record objects. There are a number of records already in that array, and these will be updated. The update options to handle are:

  • delete a given property from a record,
  • add/update a given property from a record,
  • add a track to the given record’s ‘tracks’ property.

So steps to make these things happen:

  • Is the value blank? If so, simply delete the property on the given record.
  • If the value is NOT blank, is the property NOT tracks? If so, set the property to the given value.
  • If the value is NOT blank and the property IS tracks, do we NOT have a tracks array property on the record? If we DO NOT, go ahead and make an empty tracks array property.
  • At this point, we have a value and we’re working on tracks, but either way, we now have a tracks property, so simply add this value to that array.
  • and lastly, we return the collection.

Bear in mind, the tracks property is unique, in that it’s the only property that isn’t a ‘primitive’ (were you can simply assign the value to the property), so it may make sense to remove that specialized case as early as possible, leaving the more general case for later. The pseudocode I wrote goes the other way, removing the most general case first, then dealing with more and more specific cases, until all are handled.

Thank you @snowmonkey for the ideas, I re-wrote the code entirely, turns out that it was much simpler and needs much lesser lines.

Often the case. We think things are far more complicated than they need to be. Glad it worked out, and looking forward to more great questions! :wink:

1 Like

@snowmonkey I appreciate your help, thanks. I wanna practice more what I’ve learned so far, so if you could point me on a direction where I can find projects I can work on for free to improve my skills, or maybe exercices, it would be awesome.
Also, are we aloud to ask questions only for codes in the challenges, or we can ask questions about any code in other projects ?

I don’t think there’s a strict restriction to what questions you may ask. I think the lessons/challenges here are a great way to start, but there are other ways to sharpen your skills. Some can get very deep, but I find having at few places to practice is a good thing.

Personally, in addition to working through these lessons (and I’m procrastinating, but I’ll get there), I also do some challenges (called ‘Katas’) on CodeWars - these are kind of language-specific and task-specific, great ways to hone a skillset. There are places like Codecademy, which have lessons similar to FCC’s, but slightly different (I really like the magic 8-ball challenge. :wink: ).

Get involved with Github. There are always projects to get involved with, or people to follow who are doing things you might learn from.

Get connected to either local developer groups, or online communities. Often, I encounter small study-groups within a developer community who will pose challenges to each other, to push one another to learn new skills.

1 Like