Record Collection: value not added to new property empty array

Record Collection: value not added to new property empty array
0.0 0

#1

Hey all,

I’ve been working on the stated challenge for a bit now and cannot for the life of me understand why the inputted value (when a new property has to be created) is not being pushed to the empty array given to the new property. Is it because it is empty that the value won’t be added? From what I have been reading online, push should function perfectly fine on empty arrays.

function updateRecords(id, prop, value) {
  
  if (value === "") {
    delete collection[id][prop];
    return collection;
  }  

  var testProp = collection[id].hasOwnProperty(prop);

  if (testProp === false) {
    collection[id][prop] = [ ];
  }
  
  collection[id][prop].push(value);

  return collection;
}

The challenge is telling me:

// running test
After updateRecords(5439, "artist", "ABBA"), artist should be "ABBA"
After updateRecords(1245, "album", "Riptide"), album should be "Riptide"
// tests completed

All the other tests pass just fine. Thanks for ya’ll’s time.


#2

In the first test case of updateRecords(5439, “artist”, “ABBA”), when the following if statement is executed, the condition evaluates to true, because there is no artist property in the object. So, the if code block executes which assigns an empty array to the artist property.

  if (testProp === false) {
    collection[id][prop] = [ ];
  }

Then in the next line (seen below), you push the value “ABBA” into the array. The artist property should be a string and not an array.

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

After the test case is ran, the collection looks like:

{
    "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",
      "artist: ["ABBA"]
    }
}

and the object of the “5439” property should look like:

    "5439": {
      "album": "ABBA Gold",
      "artist: "ABBA"
    }

#3

Ahhhhh, that makes sense. So it was there, it was just in the array, not as a standalone string. Thanks a bunch.