Record Collection correct answer not passing test?

Record Collection correct answer not passing test?
0.0 0

#1

Hi all,

This is not my final solution, I am just trying to pass the second test (have “Take a Chance on Me” as last element of tracks). When I run the code, everything looks correct and developer tools aren’t throwing bugs,
but the test won’t go green! Mind helping me figure out what’s happening?

Thanks - Connor

// 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) {
  // first condition - passes tests
  if (prop !== "tracks" && value !== "") {
    if (collection[id]) {
      collection[id][prop] = value;
      return collection;
    }
  // second condition - returns answer but doesn't pass tests?
  } else if (prop == "tracks" && collection[id].tracks == null)
    collection[id].tracks = [];
    collection[id].tracks.push(value);
}

// Alter values below to test your code
updateRecords(5439, "tracks", "Take a Chance on Me");

#2

Hello -
There are a couple things that you need to do - the first thing that I noticed is that for your else if condition, you don’t have curly brackets surrounding your code, so that those two lines of code are not grouped together, the one after the condition collection[id].tracks = []; will be controlled by the conditional test, the next one that pushes the value onto tracks will always be executed.

The thing that you should do is organize the actions that you are going to take based on a couple conditions. The primary one is if ( value !== '') - that’s an important thing that allows you to group a few actions together, and not have to test for the state of the value variable again. The next thing is that within that condition, if prop has the value tracks, then you can test for a null or .hasOwnProperty('tracks') and create the tracks array if needed, and then push the value. If the property is not “tracks”, then you can just set the property without needing to deal with an array.

A framework for this:

if ( value !== '') {
    // do things knowing that there is a value
    if ( prop !== 'tracks') {
        // do what you need to do for things other than tracks
    } else {
        // do the actions when you have "tracks"
    }
} else {
    // do the actions when value is ''
}

#3

Nice, thanks for the help!