Challenge is not working

I tried many ways to make my code work… for some reason it isn’t and Im not getting it.

You are given a JSON object representing a part of your musical album collection. Each album has a unique id number as its key and several other properties. Not all albums have complete information.

You start with an updateRecords function that takes an object like collection , an id , a prop (like artist or tracks ), and a value . Complete the function using the rules below to modify the object passed to the function.

  • Your function must always return the entire object.
  • If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
  • If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
  • If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.
  • If value is an empty string, delete the given prop property from the album.

Note: A copy of the collection object is used for the tests.


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

}
if (prop == "tracks" && value !== "") {
collection.push(value);
}
return object;
}
if ("value" == "") {
delete collection.prop;
}
updateRecords(collection, 5439, 'artist', 'ABBA');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74.

Challenge: Record Collection

Link to the challenge:

The issues is, that your function should take in an object like collection, but it could also be an object that is not the one you see in the challenge. So instead of ‘collection’ you should use the object from the function input in the challenge.

Working off of this, I am currently trying to solve the challenge to let you know, where the other ‘mistakes’ are, but they pretty much all stem from the fact, that you have to work with the object provided in the function and not the object that was declared in the code above.

Edit: You can think of the object above as just a nice reference to help you understand, what will get passed in to the function

Alright I basically already stated in the above response what the fundamental misunderstanding in this challenge was, so I would suggest, you pretty much start over again, at least mentally.
The reason why I’m suggesting this, is because it really isn’t that hard to solve this challenge, especially when seeing the code you’ve already written I am sure you can do it, if you now understand the fundamental misunderstanding you had. However when working with your already written code it might sort of confuse you again, but do as you feel is best.
The logic of your code is pretty much entirely correct, if not for this misunderstanding and some minor mistakes, so I would try it again and if not continue to read the following step-by-step tips (each number is directed towards each if statement (except for the one that doesn’t do anything which you can just remove)):

  1. a. In the first if statement, you should check if prop is equal to tracks. What does this mean? Is the input to the function that is assigned to prop equal to “tracks”.
    1.b If it is, you should set prop to be equal to value. What is prop? It’s a property of an objects property which has the name of id. You get all of these things(object, id, prop) from the function input. redeclare the value. In order to access prop you need to access prop within the object

  2. a. Same as 1.a. for the second if-statement.
    2.b. Similar to 1.b. : Within the condition of the if statement, you have to make sure, that the objects (you get this from the function input) property with the name of id (you get this from the function input [aka ygtftfi]) doesn’t have a property called ‘tracks’. (the hasOwnProperty part is correct, but you can/should remove what comes after it)
    2.c. Similar to 2.b. and 1.b: Create an empty array and add value (ygtftfi) to it within the object.

  3. Same as before, push value to the array within the object

4.a. Make sure to include the last if statement before you return the object.
4.b. "value" == "" will always be false, I’m sure you know why :wink:
4.c. Again, as before, you want to delete within the object (ygtftfi).

Hopefully this helped with any possible confusion.

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