Unable to understand what is missing in my solution

My all test expect one test is failing and I don’t understand what I am missing? Already it took me days to solve this but now I m stuck and do not know what to do, the first element in 2468’s tracks is 1999 but the following messsage
" After

updateRecords(collection, 2468, "tracks", "Free")

tracks should have the string 1999 as the first element. // tests completed"


// 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) {
for(let obj in object){
if(!object.hasOwnProperty("tracks") && object[id]["tracks"] !== ''){
 object[id][prop] = value;
}
if(object[id][prop] !== "tracks" ){
  object[id]["tracks"] = []
  object[id].tracks.push(value)
}
if(object[id]["tracks"] !== ""){
  object[id].tracks = []
  object[id].tracks.push(value)
}
if(object[id][prop] == ""){
  delete object[id][prop]
}

return object;
}
}

updateRecords(collection, 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/89.0.4389.90 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

Take a look at what function returns for this case, it should help with figuring out what is happening. You can add console.log call at the bottom of the code and see the result.

console.log(updateRecords(collection, 2468, "tracks", "Free"));

First of all, you don’t need that for loop. You’re returning in the first iteration anyway, it’ll only run once (and you don’t need a loop for this challenge).

Now for your problem, this is the failing test:
updateRecords(collection, 2468, "tracks", "Free")

If you run that, three of your if-conditions are true:


  • First if:
    if(!object.hasOwnProperty("tracks") && object[id]["tracks"] !== '')

If you log the object, it has no property “tracks”, and object[2468]["tracks" ] isn’t an empty string, but it’s an array with two songs in it. So at this point, you’re updating the object the first time, and you set its tracks property to Free, thus overriding the array that was there before:
tracks: 'Free'


  • Second if:
    if(object[id][prop] !== "tracks")

If you log the object, object[2468]["tracks" ] is not equal to "tracks", it’s equal to "Free" (because of what you did before). Now you set the tracks property to an empty array and push "Free" into it.


  • Third if:
    if(object[id]["tracks"] !== "")

object[2468]["tracks" ] is now an array with one item "Free" in it, so it’s not an empty string. Once again, you set the tracks property to an empty array and push "Free" into it.


This challenge is a tricky one. Just remember that object[id]["tracks"] should always be an array, never a string.

3 Likes

The output in the console just give like the following, can’t open the object as I do not know how to see it Screenshot 2021-03-28 at 15.30.06

You should be able to click on the triangle on the left and see more. However if you add that to the code on freeCodeCamp, console output should be displayed in the frame that says “Your test output will go here”.

1 Like

I did in the code

  if(object[id]["tracks"] !== ""){
    object[id].tracks = []

    object[id].tracks.push(value)
  }

isn’t this what is being asked?

So what happens, in that condition, for a collection entry that looks like:

tracks: ["foo", "bar", "baz"],

??

do you understand what the value of object[id]["tracks"] is at the beginning?

Are you referring to this?

If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it

You got the second part right, but the first part:

  • checks if object[2468]["tracks"] is an empty string
  • does not check if prop === "tracks"
  • does not check if the album has a tracks property

This is object[2468]:

2468: {
  albumTitle: '1999',
  artist: 'Prince',
  tracks: ['1999', 'Little Red Corvette']
}

I see that you are using if everywhere. Why are you not using else if?

It will add foo,bar and baz onto tracks

guess I m new, I started as one condition then saw it has 2 more so added if instead of if else onto it.

for which ID are you asking about?

No, it’ll delete that array then create a new one with just the value passed to the function.

if(object[id]["tracks"] !== ""){
    object[id].tracks = []

    object[id].tracks.push(value)
  }

So tracks: ["foo", "bar", "baz"],, tracks is not an empty string, so you replace it with an empty array then push value to it.

So if there are any existing tracks, that block of code deletes them.

And as others have said, you’re not not using else, so potentially every single one of your conditions might run.

Answer deleted from the forum

I’ve added spoiler tags to your solution. Can you please not just post solutions in future, instead please try to help guide the OP towards the solution. If the OP wanted the solution, they can see that from the challenge itself, you do not also need to post it here.

1 Like

ok i have understood. i am still learning myself

1 Like

it has been quite hard, now I m stuck on

After updateRecords(collection, 2548, "tracks", "") , tracks should not be set
whereas my code is

  if(object[id][prop] == ""){
    delete object[id][prop]
    console.log("this will be deleted"+ object[id][prop])
    }

but the property when console logged is returned undefined. feel so fustrated coming back to it after days and still not able to do :frowning_face:

Well yes, you’ve just deleted it!

If that test is failing, then need to see the rest of the code

1 Like

how are you checking if the function argument is an empty string?