Record Collection// well, at least I am still trying!

Tell us what’s happening:
I’m having good amount of difficulty understanding the instructions. I read several topics created and their solution.
I can’t say I have perfect understanding of the problem yet.

now after I wrote and corrected almost the whole thing based on what I found in other ppl solutions, I still get error
#error
After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.

I’m not sure if I’m on the right track. I don’t have any coding background and I wonder if just working with freecodecamp is good enough??
plz advise me .
tnx.

Your code so far


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

  else if (value===""){
    delete collection[id][prop];
  }

  
  return collection;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15.

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

You are currently doing this:
collection[id][prop]=[];
when prop ==="tracks" and collection[id] has member "tracks"

Did you mean to empty the array when that is true? I’m guessing not

Edit to fix grammar

would you please explain more?

In the question, it asks you to create an empty array for tracks if tracks isn’t found in the object

Instead, you’ve set the (possibly already nonempty) tracks list to be empty, whenever the tracks property exists!

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

I fix this and 2 new errors show up
//
After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.
After updateRecords(2548, “tracks”, “”), tracks should not be set

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

You have the logic of these backwards.

for the 1st, it should solve this
//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.

for the 2nd, I think it goes with this
//If prop is “tracks” and value isn’t empty (""), push the value onto the end of the album’s existing tracks array.

You’re doing the opposite.
collection[id].hasOwnProperty["tracks"] is true if the album DOES have the tracks property. Then you replace it with an empty array.

That means that your next else if only runs if the album DOES NOT have the tracks property. You then attempt to push to an undefined value, which causes an error.

ok… sorry if I seem so dumb…

now here is what I understood

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

but I end with this error

After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element

ok. thank you very much. I’ll work in the spacing.

so I have this error left to pass,

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

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

  else if (value===""){
    delete collection[id][prop];
  }

yes, correct. I’m getting the same error.

else if (value===""){
    delete collection[id][prop];
  }

isn’t this code supposed to cover this problem?

so it should evaluate to false?

and when i delete this else if, I end up with this error,
After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.

Instead of deleting/modifying/adding new code, I suggest you write out your algorithm for this challenge first in plain language. Write down the exact steps you would take if you were the computer. That means you need to make sure your logic will work on paper with each test case before writing ANY code. At this point you are just guessing at a solution by changing bits of code here and there, instead of understanding what the algorithm should be for you to be able to write the correct code.

An algorithm is just the logical step by step process you need to perform to get to the correct response.

so this how I edited it

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

how is that?

I wonder if you could advise me, what should I do beside freecodecamp to improve my skills.