Record Collection ChrisH1

Hi I’m having the same problem (or so I think). I cannot get a new property to be created with the name “tracks” inside the id property of recordCollection. I’ve tested it again and again and it’s always this line that throws up the problem:

if ((prop == "tracks") && (records[id].hasOwnProperty("tracks")) == false)
{
      records[id]["tracks"] = new Array(value);
 }

And as per the instruction:

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

Not getting any syntax errors either. I’ve tried many variations on the line:

records[id][“tracks”] = new Array(value);

Many, many variations including using push and unshift commands. I’ve left this one in for now as it’s the closest to the instruction’s requirement. Try as I might I cannot get a new property created in the id 5439 of ‘tracks’, and this is the error it throws up every time.

Has anyone got any insight?

Thanks :slightly_smiling_face:

Can you share your full code? That is an uncommon way to declare an array, but it should be fine.

Side note - the weak comparison operator == should generally be avoided… Use ===

Ok here it is thanks:

// 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 ((prop != "tracks") && value)
  {
      records[id][prop] = value;
  }

  if ((prop == "tracks") && (records[id].hasOwnProperty("tracks")) == false)
  {
      records[id]["tracks"] = new Array(value);
  }

  if ((prop == "tracks") && value)
  {
      records[id]["tracks"].push(value);
  }

  if (!value)
  {
      delete records[id][prop];
  }

  return records;
}

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

These both will run under some cases

Thanks Jeremy, I tried both and still no dice. I’m off to bed now I’ll check back tomorrow to see if there are any more replies thanks :slightly_smiling_face:

‘tried both’ of what? I highlighted two if statements in your code. Sometimes these two if statements will both execute. You don’t want that.

Ah I understand what you mean now sorry. I’ve have another look tomorrow - getting awfully tired now lol!

1 Like

Sleep is good for coding!

Thank you very much! :grinning:

Lol it’s fixed. Tried a random thing based on your suggestion Jeremy as follows:

if ((prop == "tracks") && (records[id].hasOwnProperty("tracks")) === false)
  {
      records[id]["tracks"] = new Array(value);
  }  
  else if ((prop == "tracks") && value)
  {
      records[id]["tracks"].push(value);
  }

The else if fixed it and I assume prevented both if statements running. Thanks for that! :slightly_smiling_face:

1 Like

Yep, that’s what I was getting at. Generally, if you have a series of if statements and only once case should run, you want if-else all the way

It’s a deal I’ll try and update my coding approach accordingly. Thanks very much for your help. Got to get some sleep now, I have a heavy shift on the dishes tomorrow at work - not looking forward to it lol. Bye for now :slightly_smiling_face:

1 Like