Record Collection - repl.it the results are all correct, but code is not passing thru in FCC

Tell us what’s happening:

What I did wrong?
when I am checking my code:
1 After updateRecords(2468, "tracks", "Free") , tracks should have "1999" as the first element. I have it as the 1st element

  1. After updateRecords(2548, "tracks", "") , tracks should not be set, I cannot see tracks for that id, why it is not going thru?

thanks in advance

Nancy

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 isn't "tracks"and value isn't empty (""), update or set the value for that record album's property.
if(prop !== "tracks"&& value!==""){
  collection[id][prop]=value;
}  
//If prop is "tracks" but the album doesn't have a "tracks" property, create an empty array before adding the new value to the album's corresponding property.
else if(prop === "tracks" && value){
collection[id][prop]=[];
collection[id][prop].push(value);
}
//If  prop is "tracks"and value isn't empty (""), push the value onto the end of the album's existing tracks array.
else if (prop === 'tracks' && values !== ""){
  collection[id][prop].push(value);
}  
//If value is empty (""), delete the given prop property from the album.
else{
  delete collection[id][prop];
}
  return collection;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

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

else if (prop === 'tracks' && values !== "")

values, it should be value. But even if you fix it, that condition is exactly the same as the previous condition:

else if(prop === "tracks" && value)

More importantly, none of the conditions actually check the record collection object, all you are doing is checking the values put into the function, you never look at the object to see if it has a specific property. It may well give the result you want for a specific input, but that is accidental.

This worked for me. You can try it tooo. Hope it helps
// 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 isn't "tracks" and value isn't empty (""), update or set the value for that record album's property.*/
 if (prop !== "tracks" && value !=""){
  collection[id][prop]=value;
  //collection.id.prop = value;
/*If prop is "tracks" but the album doesn't have a "tracks" property, create an empty array before adding the new value to the album's corresponding property.*/
 }else if (prop =="tracks" && collection[id].hasOwnProperty(prop) == false){
  collection[id][prop]=[];
  collection[id][prop].push(value);
  //collection.id.prop = [];
  //collection.id.prop.push(value);
  /*If prop is "tracks" and value isn't empty (""), push the value onto the end of the album's existing tracks array.*/
 }else if (prop == "tracks" && value !==""){
  collection[id][prop].push(value);
  //collection.id.prop.push(value);
 }else{
    delete collection[id][prop];
    //delete collection.id.prop;
 }
  return collection;
}

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

Don’t always follow the order of the requirements directly. Think about it a bit first. What is the common element between all the cases? checking for an empty value. so we know that will apply to all props. Deal with that first. Next, which ones have something in common? artist and album will have the same action no matter what. so deal with them. Then all you have left is tracks to do the funny business with:

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

  return collection;
}
1 Like

thanks for support and help

1 Like

thank you, this one looks more logic :slight_smile: