Record Collection S.O.S

Tell us what’s happening:

The code doesn’t seem to hit all the checkpoints. My method is different from the given answer but I don’t see why it shouldn’t work. Help needed

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.hasOwnProperty ){
      if (value !== ""){
       collection[id][prop] = [value];
     }else  if ( collection.hasOwnProperty == false ){
      
        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 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

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

DELETED SECTION!..

That’s not how you use hasOwnProperty

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

Not sure if thats how it goes but the code still doesn’t work through the 2nd to last and 3rd to last checkpoints.

What exactly do you think the above is checking? If you want to check if an object as a property, you can simply write it like:

var obj = {
  someProperty: 'a value',
  someOtherProperty: 'another value'
};

// check if obj has a property named 'someProperty'
if (obj.hasOwnProperty('someProperty')) {
  console.log('it does');
}

// The above check will be true, so 'it does' will be displayed in the console.

A previous comment sent me to a web that mentioned that " JavaScript does not protect the property name hasOwnProperty ; thus, if the possibility exists that an object might have a property with this name, it is necessary to use an external hasOwnProperty to get correct results:". Which makes sense since the program might run on the basis that hasOwnProperty is a property were trying to access through the dot notation used to access properties.

  1. Also the directions ask to check if it doesn’t have the property

That could be said about almost all JavaScript object/array methods and properties. There is no reason to do that for this challenge. Just stick with the built-in hasOwnProperty method and you will be fine.

var obj = {
  someProperty: 'a value',
  someOtherProperty: 'another value'
};

var propNameToCheck = 'someNonExistentProperty';


// check if obj has a property with same name as the value of propNameToCheck
if (!obj.hasOwnProperty(propNameToCheck)) {
  console.log('it does not');
}
// The above check cause 'it does not' to be displayed in the console.

Thanks, but I already had it that way and it wasn’t proving all checkpoints(2nd and 3rd to last). Any other suggestions to what the problem might be?.

Help appreciated.:grinning::smiley:

What code version (the first one you posted or the latest using the convoluted version of using hasOwnProperty with the call method) ?

Both versions weren’t working. same checkpoints weren’t working

{
  '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'
  }
}

Your latest version causes the above collection object to be returned for the following test case:

updateRecords(2468, "tracks", "Free");

You have two problems in the section of code below:

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

If prop is “tracks”, which it is in the test case above, your nested if statement is asking if there is NOT a property named “tracks” in the collection object and this evaluates to true. There is no “tracks” property in the collection object. Instead, there are property names like ‘1245’, ‘2468’, ‘2548’, and ‘5439’.

Also, if the very next else if statement evaluates to true at any point, you are going to get an error, because Collection is not defined anywhere in your code.

Thanks, that makes sense. I’ll check the internet to look for ways to make it so that it evaluates the property inside of the id object.(checking for properties in nested objects)
I also fixed the capitalization error.

Hint: You already know how to reference a nested collection object (see above).

oohhhhhhh, I could just see if the result of collection.[id][prop] yields undefined. I was testing for nested objects instead of checking for undefined. wow
Thank you so muchhhh. I was stuck on that forever.