Record Collection! need a hand

Record Collection! need a hand
0

#1

Tell us what’s happening:
So, I finished the function and it’s working. But I can’t understand why it doesn’t create an array. or if it does it doesn’t push the string onto the end. Anyway, will appreciate any help!

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" && value !== ""){
    collection[id][prop] = value;
    
  } else if (prop == "tracks" && collection[id].hasOwnProperty("tracks") == false) {
    var array = [];
    collection[id][prop] = array;
    
  } else if(prop == "tracks" && 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");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

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


#2

So your code works except for one test, correct?

After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.


#3

I think the problem is that when someone tries to add something to the non-existing tracks array your code will go into the if statement that creates the array but will never enter the code area that adds to that array.

Using if elses is the problem then since these two checks are not mutually exclusive (they both can occur in a single function call):
(prop == "tracks" && collection[id].hasOwnProperty("tracks") == false)
and
(prop == "tracks" && value !== "")

So given this, can you think of a way to rewrite your code to handle this scenario?
I hope I’ve set you on the right path…


#4
} else if (prop == "tracks" && collection[id].hasOwnProperty("tracks") == false) {
    var array = [];
    collection[id][prop] = array;
    

This line says, “if the prop to add is tracks, and collection[id] doesn’t have the prop tracks, then add an array to collection[id][prop]”

The reason your array is not updating with the correct value is here.

I was purposely vague so not to give you a spoiler. Let me know if there’s anything I didn’t make clear enough.


#5

Thank you!
solution was
collection[id][prop] = [value];


#6

I think, chaining if else would solve the problem.


#7

Just nest the second if to check if value is empty or not inside the if to check if tracks is the prop etc.

That way when you create the new array, you can also push the new value into it. Glad you solved it.