Record Collection 2nd and 3rd test case

Record Collection 2nd and 3rd test case
0.0 0

#1

**the 2nd and 3rd case i failing any insight as to what i am not seeing?
After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.
After updateRecords(2548, “artist”, “”), artist should not be set
**

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

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0.

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


#2

I had a quick look at the second test case (maybe the problem with the third case is similar, I haven’t checked it yet).
The problem is situated in the below part:

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

The id has no property called tracks, so your function makes one with an empty array. Which is a good start. However you never end up pushing the value to your new array as your “if function” ends after creating an empty array. One quick fix without changing too much of your code would be as follows:

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

See if the above can help you figure out the problem with your last test.


#3

Hi,
In the case of second test your code makes an empty array but never adds the property
updateRecords(5439, “tracks”, “Take a Chance on Me”)

if(!collection[id].hasOwnProperty(prop))
    {
      collection[id][prop] = []; //created array but added nothing to it
    }

In case of the third test your code does not handle the situation of empty value.
updateRecords(2548, “artist”, “”),

if(value!==''){
      collection[id][prop] = value;
    } // what happens if value==='' ?

Good luck


#4

Yup got the solution thanks for the help