Record Collection Challenge - Only one instrucion failing

Record Collection Challenge - Only one instrucion failing
0

#1

Hi, guys!
I have been stuck for over a week on this challenge. After going on youtube to check problem explanations and quite a bit of research on the freecodecamp forum I could get somewhere close to the solution.
By now, I only fail on one of the instructions. In the following:

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

I can’t seem to understand why ain’t it working. I stare at the screen without finding the logic behiand the error.
Any suggestion or guidance will be much appreciated.
Thanks a lot in advance!

Here it goes the code:

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

  return collection;
}

#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

The problem is your logic for adding new tracks for an album. That logic is seen below:

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

In the test case of updateRecords(2468, “tracks”, “Free”), the else if above evaluates to true, so your next if statement (seen below) checks to see if the collection object DOES NOT have a property named ‘prop’. Well, it does not have such a property. The only properties in the collection object are the following: ‘2548’, ‘2468’, ‘1245’, ‘5439’, so your you end up setting collection[id][prop] (which is really collection[‘2468’][‘tracks’] to a blank array and then you push ‘Free’ to the array. At this point the tracks property of the ‘2468’ object looks like [‘Free’].

 (!collection.hasOwnProperty('prop')) {

Then after the if statement you push the same value (‘Free’) in the the tracks array for the ‘2468’ object. After this line, the tracks array looks like [‘Free’, ‘Free’] when it should looked like [‘1999’, ‘Little Red Corvette’, ‘Free’].


#3

This is one tricky Challenge!

I wrote about it back in 2016, and since the new update and test suite, the previous answers & solutions has stopped working. I plan to review, revise, and update the post, however I’m currently really busy with another project and can only share the 1 solution that works, (as of JUNE 9, 2018) … which you will find in the comments section, … just scroll down till you come to the comments starting on JUNE 9, 2018 & recently, share a few more that works, SEE comments with today’s Date: JUNE 19, 2018


#4

wrong:collection.hasOwnProperty(‘prop’)
1.
prop is argument, should be variable
‘prop’:string, ===‘prop’
prop : variable
2.
collection 's properties :“2548”、“2468”、“1245”、“5439”
has no “tracks”。


#5

My Updated solution below.

Note that in my 2nd “else if” argument:
else if (prop==“tracks” && collection[id].hasOwnProperty(“tracks”)==true && value !=="")

, that I added:
value !==""

The reason is because the “Run the Tests” button failed on the following checkpoint:
After updateRecords(2548, “tracks”, “”), tracks should not be set

When my line of code was written:
else if (prop==“tracks” && collection[id].hasOwnProperty(“tracks”)==true

My code would execute on the above else if statement, instead of getting to:
else if (value==""){
delete collection[id][prop];

which would delete the prop or “track”.

I hope that makes sense.

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


#6

It is fine to post suggestions and hints as you have done, but please avoid posting full working solutions in the future. Your solution has been blurred.

Thank you.


#7