Basic JavaScript - Record Collection

Tell us what’s happening:

The following occurs when I run the test:

After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me") , tracks should have the string Take a Chance on Me as the last and only element.

But I am unsure why this is the case since I’d thought that .push() method would always add an argument to the end of the targeted array.

I’d greatly appreciate any advice or suggestions, so thank you in advance!

Edit: Changed records[id].hasOwnProperty["tracks"] to records[id].hasOwnProperty("tracks") because .hasOwnProperty() uses parentheses.

Your code so far

// Setup
const recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
  //If value is an empty string, delete the given prop property from the album.
  if (value === "") {
    delete records[id][prop];
  } 
  //If prop isn't tracks and value isn't an empty string, assign the value to that album's prop.
  else if (prop !== "tracks" && value !== "") {
    records[id][prop] = value;
  } 
  //If prop is tracks and value isn't an empty string, you need to update the album's tracks array. 
  else if (prop === "tracks" && value !== "") {
    // First, if the album does not have a tracks property, assign it an empty array.
    if (records[id].hasOwnProperty("tracks") === "") {
      records[id][prop] = [];
    }
    //Then add the value as the last item in the album's tracks array.
    records[id][prop].push(value);
  }
  //Your function must always return the entire records object.
  return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Your browser information:

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

Challenge Information:

Basic JavaScript - Record Collection

This will never happen though. hasOwnProperty never returns an empty string.

Try running the failing test case to get more clues.

It was my understanding that the === is used for comparisons, so doesn’t this line mean that there is no “tracks” property? If not, would a better value be false to indicate that the album doesn’t have a “tracks” property?

Because even after running the test, this is all I get in return:

// running tests
After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"), tracks should have the string Take a Chance on Me as the last and only element.
// tests completed

It is also a method () not []

.hasOwnProperty("tracks")

Thank you for pointing that out! I didn’t even realize I used brackets instead of parentheses.

However, I’m still getting this:

// running tests
After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"), tracks should have the string Take a Chance on Me as the last and only element.
// tests completed

As was pointed out it does not return something you can compare to an empty string, it returns true or false

if (object[prop].hasOwnProperty("someProp"))

// or

if (!object[prop].hasOwnProperty("someProp")) 

Run the failing test yourself instead of just letting the test suite run it. You’ll get an error message.

You aren’t actually creating an array because hasOwnProperty never returns string. You can Google ‘MDN hasOwnProperty’ to see what it does, or check the lesson where it’s covered. hasOwnProperty returns a Boolean value

What would you recommend to a beginner in terms of testing the JS code myself? Preferably free-to-use.

You run the code in the same place where your are typing it. Totally free!

Right now you have this sample function call.

OH! I forget I can do that. :sweat_smile: And here I was Googling how to run JS codes for free. :person_facepalming: Thank you so much for your prompt and detailed help! The link you attached was very helpful in better understanding the .hasOwnProperty() method and it was definitely because my code wasn’t checking if the property returned false. Thanks again! :pray:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.