Basic JavaScript - Record Collection, cannot pass one test

Following my last post here I have updated code with console log, I can see the value is being added but I m not passing one test

// 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 === '')
    delete records[id][prop];
  if(prop !== 'tracks' && value !== ''){
    records[id][prop] = value
  }
  if(prop == 'tracks' && value !==''){
    if(!records[id].hasOwnProperty('tracks')){
      console.log(" checking for tracks prop ", records[id])
      records[id].tracks = [];
      console.log(" checking for tracks and empty array ", records[id]);
      records[id].tracks.push(value);
      console.log("checking for value as array", records[id]);
    }
  }
  return records;
}

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

The test which I cannot pass is

After updateRecords(recordCollection, 1245, "tracks", "Addicted to Love") , tracks should have the string Addicted to Love as the last element.

Can you walk through how this logic matches the prompt

Note - it really helps if you include the link to the Challenge since we don’t have them all memorized :upside_down_face:

My bad.Here it is

What do u mean?

If prop is tracks and value isn’t an empty string, you need to update the album’s tracks array.

First, if the album does not have a tracks property, assign it an empty array.

Then add the value as the last item in the album’s tracks array.

Can you describe how your code meets these 3 sentences?

The last function requirement has two conditions. One is explicitly stated which is the one you got correct and one is kind of implied (pretty sure I argued for it not being so in an issue at one point).

  1. It doesn’t have the tracks property, so you need to add the property before you can add the value. You got this one.

  2. It does have the tracks property, so you can just add the value to it. This one is missing. A simple else on the nested if would do it.

In english it says

  1. if prop == tracks and value is not a empty string then go inside 2
  2. if records with the id does not have a property named tracks
  3. then add a property name tracks and assign to empty array
  4. then push the value into the array

but I covered that when I do

 if(!records[id].hasOwnProperty('tracks')){
      records[id].tracks = [];  //here I am assigning tracks a empty array
      records[id].tracks.push(value);  //then I am pushing the value in it
}

This is where you go wrong.

This sentence isn’t part of the "if the album does not have a tracks sentence.

I didn’t understand, what you mean

If the tracks array already exists and you assign it an empty array no matter what, what happens to the data in that array?

Your code effectively says “only update the tracks array if there was no tracks originally and ignore the value to add if there is already a tracks array”. That’s not what the instructions are looking for. You should always add the value to the tracks array if prop is “tracks” and the value isn’t “”.

it will be wiped and new array will be written to it?

To put it another way, the instructions say this must always happen

when this happens

even if this isn’t true

How can you push to an existing array if your condition says it has to not exist?

so I updated it but still same issue

 if(prop == 'tracks' && value !==''){
    if(!records[id].hasOwnProperty('tracks')){
      records[id].tracks = [];
    }else{
      records[id].tracks.push(value);
    }

this is the part I want to give up. how can i change my mindset by deconstructing the problem even more?

Why the else?

If you hide this behind an else, then it won’t always happen.

I thought if it wasn’t a prop then give it, otherwise (else) it mean it has that prop but we need to push it. I solved it now but yet I m so confused with instructions and why I do not get it

This is the first challenge where the instructions aren’t ‘repeat the example’. Learning how to translate written descriptions into code is hard.

What is ur advice and how to breakdown probelm to understand it better?

Lots and lots of practice and talking to people when you’re stuck.

1 Like