Can also be done with hasOwnProperty method

Tell us what’s happening:
i am trying to rrun this code but getting error of 2 nd case
but in other editor its working fine

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

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

Your browser information:

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

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

What does the failing test say?

Try debugging – add the following line just before you return your collection at the end of updateRecords:

  console.log("id:",id,"prop:",prop,"value:",value,"collection:",JSON.parse(JSON.stringify(collection)));

That will tell you each property you start with, and the ending value of collection. I can tell you, if tracks is not a property on the given member, you’re doing something wrong.

I am getting this output by logging what you said and i have ran function with following parameter
updateRecords(5439, “tracks”, “ABBA”);


“id:”
5439
“prop:”
“tracks”
“value:”
“ABBA”
“collection:”
[object Object] {
1245: [object Object] {
artist: “Robert Palmer”,
tracks: []
},
2468: [object Object] {
album: “1999”,
artist: “Prince”,
tracks: [“1999”, “Little Red Corvette”]
},
2548: [object Object] {
album: “Slippery When Wet”,
artist: “Bon Jovi”,
tracks: [“Let It Rock”, “You Give Love a Bad Name”]
},
5439: [object Object] {
album: “ABBA Gold”,
tracks: [] // i was supposed to get this output right???
},
id: 5439,
prop: “tracks”
}

let’s see your code (I reformatted it a little bit to make it more JS and less C while I was playing with it):

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

first off, as you can see I commented out those last two lines. All they do is add a prop called “id” with the value of whatever was in id and a prop called “prop” with the value of whatever was in prop to the collection object. It doesn’t break the tests, but it is a very bad and dangerous thing to do when you don’t mean to do it.

Now, to solve your actual problem. Consider how if/else if/else flows:

if (fred is true) {
    do a thing then break out of the if statement
} else if (bob is true) { // only if fred was false
    do a different thing then break out of the if statement
} else { //only if both fred and bob were false
    do something else then break out of the if statement
}
do this thing after breaking out of the if statement

so consider that and look into what you are doing when “track” doesn’t exist.

2 Likes

So take a look at that the last entry, 5439. It did not have a tracks property, so the first branch of your if statement applies. What EXACTLY is that if block doing?

  1. It creates an Array stored in a variable that you don’t need and never use.
  2. It creates the tracks property on the right object, identified by id.

And… That’s it. Do you see someting missing there? If the tracks prop exists, you push the passed value onto the Array, but if you create the array that value is just… Poof-gone.