Record Problem .hasOwnProperty issue

Tell us what’s happening:
My code passes all the tests except updateRecords(5439, "artist", "ABBA");
When I run the code in a text editor and Chrome it passes this one.
I can not understand what is wrong with .hasOwnProperty in here?!!!

Thank you

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

// Only change code below this line
function updateRecords(id, prop, value) {
   if (value ==="") {
     delete collection[id][prop];
   } else {
      if (prop !== "tracks") {
        collection[id][prop] = value;
      } else if (collection[id].hasOwnProperty("tracks") === false){
        collection[id][prop] = [];
        collection[id][prop] = value;
      } else {
        collection[id][prop].push(value);
      }
   }
 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/78.0.3904.108 Safari/537.36.

Challenge: Record Collection

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

Welcome to the forum :slightly_smiling_face:

The test you mentioned actually passes for me. It is the one right after that one that fails:

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

If you need help with this one try to log the tracks property. If you compare it to the other objects in the array and consider the error message (“the last element”) you should find the solution.

Let me know if it helps.

Sorry!
I made a mistake it does not pass this test: updateRecords(5439, "tracks", "Take a Chance on Me")

No problem :+1:

Have you tried to look at how the track is added to the collection? Consider that the value should be the last item of the tracks property, so it expects a clear data type.

1 Like

I did the test again and this is the output on Chrome console:

{1245: {…}, 2468: {…}, 2548: {…}, 5439: {…}}
1245: {artist: "Robert Palmer", tracks: Array(0)}
2468: {album: "1999", artist: "Prince", tracks: Array(2)}
2548: {album: "Slippery When Wet", artist: "Bon Jovi", tracks: Array(2)}
5439:
album: "ABBA Gold"
tracks: "Take a Chance on Me"
__proto__: Object
__proto__: Object

Compare the tracks property for your object 5439 compared to the tracks property of the other objects. The tracks are described in a specific data structure.

Sorry for being cryptic, but I’m trying to guide you more than giving the solution directly.

1 Like

Yesssss,
Thank you. I really appreciate your help.
To add an element to a new empty array I need to use .push.
This one passes all tests.

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

Thanks again

Now the output in console is:

1. {1245: {…}, 2468: {…}, 2548: {…}, 5439: {…}}

  1. 1245: {artist: "Robert Palmer", tracks: Array(0)}
  2. 2468: {album: "1999", artist: "Prince", tracks: Array(2)}
  3. 2548: {album: "Slippery When Wet", artist: "Bon Jovi", tracks: Array(2)}
  4. 5439:

    1. album: "ABBA Gold"
    2. tracks: ["Take a Chance on Me"]
    3. __proto__: Object

  5. __proto__: Object

which tracks is an array.

Yes :+1:

Glad to have been of some assistance