Record Collection...What is wrong with this code?

Hi,

I have been stuck with this for 2 weeks and then copied an example code from another thread, but something is still wrong. How could I make this code right?


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

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA", "Take c Chance on Me", "Addicted to Love");

Just as a note, you’re calling the function incorrectly.

updateRecords(5439, "artist", "ABBA", "Take c Chance on Me", "Addicted to Love"); is not a valid call based on the problem description. The tests in the problem are:

updateRecords(5439, “artist”, “ABBA”)
updateRecords(5439, “tracks”, “Take a Chance on Me”)
updateRecords(2548, “artist”, “”)
updateRecords(1245, “tracks”, “Addicted to Love”)
updateRecords(2468, “tracks”, “Free”)
updateRecords(2548, “tracks”, “”)

It looks like it would work anyway because the last two parameters are just ignored.

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

The main problem I saw was you were assigning a string to tracks:. The problem wanted an array. Basically, yours was right, except instead of returning

tracks: [<string>, <string>, ...] it was returning tracks: <string>.

FYI, this line says set the collection[id][prop] to itself if it’s defined, set it to an empty array if not.

collection[id][prop] = collection[id][prop] || [];

Hi,

I changed the code a bit. Everything seems to be ok, but one thing is wrong. It announced that: “After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.”


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

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

There was one more line. It should look like this:

collection[id][prop] = collection[id][prop] || [];
collection[id][prop].push(value);

First make sure the array is initialized, then push in the value.

Hi,

It still says that the same part is wrong: “After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.”

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

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

Must add collection[id][prop] = collection[id][prop] || ; to your second arr.push() method

1 Like

But it already has “tracks” property? I mean this: “If prop is “tracks” and value isn’t empty (”"), push the value onto the end of the album’s existing tracks array."

I really don’t understand what to do.

Your else if statement (prop === “tracks” && value !== “”) means that the property “tracks” must have a value. The challenge requires you to add a track to an artist which currently has no artist property.

collection[id][prop] = collection[id][prop] || []; declares collection[id][prop] to be the value of itself, if there is an artist property, or to have no value if there is no artist property.

1 Like

Thanks :slight_smile: Now I got it right. So it is simply as that: there must be an “artist” property to declare/return the value from “tracks” property?