Record collection challange

I am having trouble on record collection challange in Javascript i tried the chat several times but no one noticed me so i am posting it here.

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

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

My problem i think is that i am not adding new array correctly. the error i get is.

After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.

5 Likes

I am having the exact same problem. This challenge is really confusing. I am not even sure what it’s asking us to do. I hope someone can help.

12 Likes

if you find a way to work around don’t forget to post it here too. same here if i find a way il post it here too.

1 Like

Things I noticed:

  1. the line collection[id][prop].new Array(); is wrong
  2. else (value === "") { shouldn’t have a condition
  3. if you have prop === 'tracks' and there is no tracks, you should create an array

Your code edited:
SPOILER ALERT!!!

function updateRecords(id, prop, value) {
    if (prop !== "tracks" && value !== "") {
        collection[id][prop] = value;
    } else if (prop == "tracks" && value !== "") {
        if (!collection.hasOwnProperty("tracks")) { // moved from outside loop
            //collection[id][prop].new Array();  // wrong syntax
            collection[id][prop] = [];
        }
        collection[id][prop].push(value);
    } else { // removed (value === "")
        delete collection[id][prop];
    }
    console.table(collection);
    return collection;
}

:rotating_light: :rotating_light: :rotating_light: :rotating_light: :rotating_light:

EDITED 12. Aug.:

Since I posted this answer, the challenge has been changed so this code doesn’t work anymore!!!

3 Likes

it worked thanks a lot.

I tried using the code from the wiki and it didn’t work. You should consider updating the article.

1 Like

I found this challenge to be the most difficult. I hope they break it up into small challenges as it is just not explained properly. I passed the challenge but still don’t understand how the code worked.

7 Likes

could you explain what this line does?

1 Like

It is like a dream within a dream.

Example:

updateRecords(5439, "artist", "ABBA"); // updateRecords(id, prop, value);
var collection = { // <== First level "collection"
...
...
    5439: { // <== Second level "id"
        album: "ABBA Gold" // <== Third level "prop"; value is "ABBA`
    }
};

So the line collection[id][prop] = value; goes to a variable collection, then searches for property named id (5439) and inside that id property searches for a prop property (in this case artist). If there is one, it assigns the value of value if there isn’t, it creates new name: value pair (in this case artist: "ABBA").

3 Likes

Hi i found this challange difficult as was not understanding properly what needed to be done, thankfully this forum has cleared it up. the above code fails and if you look at the following line from the code i will explain why…
if (!collection.hasOwnProperty(“tracks”)) { // moved from outside loop

its checking to see if the collection has the property “tracks” and then using the ! to flip the answer eg if true changing to false … if false change to true; problem its always going to be false fipped to true. As collection dosent have a property “tracks” Collection properties are the id eg 2548 2468 etc …
its the ids that have or have not the property “tracks” eg 2468 has the property “tracks” 5439 dose not have the property "tracks"
so if you change slightly the line of code i have in this post youll fix the problem
A big help is to create a file that you can debug using the developer tools online so you can step through the code. This is how i figure out whats going on and where im going wrong.So what i did was create a html file linked to a js file … open in browser and then i can debug and step through the code to see whats going on. This makes doing tricky challenges so much easier.

4 Likes

You won’t believe, I am stuck with the challenge for the entire day today. :frowning2:

5 Likes

if you need some help post what you have got and explain where your stuck ill be around every so often and would be willing to help as im sure others would also.

1 Like

Thanks John! :smiley:

I was able to clear it after reading this page. It ws a really difficult one and the instructions need to be modified for clarity, I think.

8 Likes

JohnL3, thanks for clarification, without you, I’d still be struggling trying to finding out what outcome should be.

1 Like

I really struggled with this one myself. There was one little bit of code that prevented my function from working which is this:

if (!collection[id].hasOwnProperty("tracks")) {

I did not enter the [id] after collection so I kept getting an error. Whew, glad that one is solved. Here is my entire function for reference.

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

  return collection;
}
16 Likes

I’ve read through your code and must say, I don’t have a clue anymore.
It’s like I’m going backwards.

5 Likes

Thank you for your answer, it works really well.

is there any way we can do beside the function .hasOwnProperty? I tried to use to detect the “tracks”'s length to find out if it has “tracks” there, but it didn’t work.

2 Likes

I think the part in the description where we have to create an empty array:
"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."
should be more like this:
“If prop is “tracks” and value isn’t empty but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.”

I think I understand what you’re doin here, and so I also used your code… all tests pass except for:

After updateRecords(2468, "tracks", "Free"), tracks should have "1999" as the first element.

If you see the new table, it looks like this:

collection = { "1245": { "artist": "Robert Palmer", "tracks": [] }, "2468": { "album": "1999", "artist": "Prince", "tracks": [ "Free" ] }, "2548": { "album": "Slippery When Wet", "artist": "Bon Jovi", "tracks": [ "Let It Rock", "You Give Love a Bad Name" ] }, "5439": { "album": "ABBA Gold" } }

Notice how "2468": { "album": "1999", "artist": "Prince", "tracks": [ "Free" ] } , the tracks have been erased and then “Free” was pushed. how did you get this to work for you?

nvm, I see what you were talking about on ln 37… it works now. ty very much!