Record Collection trouble

I have done everything and I can’t seem to see the problem.

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

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

Your function seems correct. I am not sure if you meant to cut off part of the collection object definition in your post.

Do you see any errors showing in the browser console (Ctrl + Shft + J)?

I do not have the collection object definition. That is exactly what the error was about.

fix that, or you will get the wrong syntax error. if you don’t remember what it was, copy somewhere else all the code in the editor and reset the code

It says to not change the collection object’s initialization. What is that?

the collection object is initialised in the code . I don’t see the initialisation part in your code (initialising a variable means giving it a value in the same line it is declared). You are actually missing the part in which the object is stored in the variable, if that is your whole code
if you reset the code you should see it whole

I am still having trouble with the Record Collection collection initialization which is the same thing.

If you are still having trouble with this challenge, please post your latest code attempt and explain what errors you are seeing in the browser console and/or what tests you are not passing.

You should not change the collection object's initialization
is what I am having trouble with. I don’t know how to initialize a variable.

What does you code look like?

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

// Alter values below to test your code
updateRecords(2468, "tracks", "test");
console.log(updateRecords(5439, "artist", "ABBA"));
var collectionCopy = JSON.parse(JSON.stringify(collection));

Why do you have this line? The challenge does not start with this code. You are only supposed to change the code below the comment which states “Only change code below this line”.

I took it out now. I got that from people on the internet doing that.

many challenges has just been changed this week, old advices may not be valid anymore. Even the guide still needs to be completely updated

but yeah, if you still have troubles, post your last code

Here it is:

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"
    }
};

// Only change code below this line
function updateRecords(id, prop, value) {
  if (value === "") {
    delete collection[id][prop];
  } else if (prop === "tracks") {
    collection[id][prop] = collection[id][prop] || [];
    collection[id][prop].push(value);
  } else {
    collection[id][prop] = value;
  }
  
  return collection;
}

// Alter values below to test your code
updateRecords(2468, "tracks", "test");
console.log(updateRecords(5439, "artist", "ABBA"));

@TheMightyRa I suggest you click the Reset Challenge button, because the collection object you show above is not exactly the same as the one shown in the challenge seed code. You only should be changing the function code.

100% working …
Follow the steps in the above solution

  1. copy the collection and do all the operations in that copied collection.
    you can use the following for copying the collection

var collectionCopy = JSON.parse(JSON.stringify(collection));

  1. at the end return the copied collection,

return collectionCopy;

  1. while deleting delete in original collection as well as copied collection.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Also, please do not revive old threads simply for the purpose of posting solutions.

Thank you for understanding.

1 Like

I was also facing the problem in this step and even after i see the solution i could not get the right answer thats why i wrote the solutio. But as per your advice i already edit that and instead of writing the whole solution i wrote the steps to be follow, i hope this will help :slight_smile:

When you first load the problem, the Collection Object has the following property.

....
1245: {
    artist: "Robert Palmer",
    tracks: [ ]  <--- *Notice the Space in the Array.*
  },
....

But when you try to format your code, by pressing the (ctrl+shift+i),
the formatter removes that space .

Therefore changing the original initialized collection Object.

that’s why you get the ERROR: ( You should not change the collection object’s initialization).

Solution: Before submitting your code, check if your object is completely the same as the original object. (even the spaces between empty Arrays)