Record Collection: One last part of the code I'm having trouble with

Hi Guys,

Thanks for your time in taking a look at my code! Here’s the link to the challenge:
(https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection)

So I’ve thankfully managed to fulfil most of the criteria of the challenge (thanks to Hint 3) EXCEPT for one: " After updateRecords(recordCollection, 2468, "tracks", "Free") , tracks should have the string 1999 as the first element."

After doing a console.log, I can see that tracks: [Free] has been pushed into the array, but i don’t understand why I have somehow overwritten the array instead of adding to it… I can’t seem to find a similar forum post hence, sorry for the trouble :sweat_smile:

// Setup
var 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;
  } else if (prop === "tracks" && records[id][prop] !== "tracks") {
    records[id][prop] = [value];
  } else if (prop === "tracks" && value !== "") {
      if (records[id].hasOwnProperty(prop) === true) {
        records[id][prop].push(value);
      } 
  }
  
  if (value == "") {
    delete records[id][prop];
  }

  return records;
}

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

I suspect that this is not what you think it is.

Hey Jeremy!

Ah alright so I thought I was checking if the record collection had a “tracks” property but what I was actually checking was if there were any properties named “tracks”… I think, lol.

So redid that portion of the code and the error got transferred to the “hasOwnProperty” function instead :sweat_smile: so i’m guessing my nested if to check for the hasOwnProperty method isn’t working as it should:

// Only change code below this line
function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && value !== "") {
    records[id][prop] = value;
  } else if (prop === "tracks" && records[id][prop] === "") {
    records[id][prop] = [value];
  } else if (prop === "tracks" && value !== "") {
      if (records[id].hasOwnProperty(prop)) {
        records[id][prop].push(value);
      } 
  }
  
  if (value == "") {
    delete records[id][prop];
  }

  return records;
}

the two things you say here are actually the same, and none of the two is what was happening
you were checking if the value of record[id][prop] is different from "tracks", which is always true

Thanks for clarifying ilenia! I was getting more confused by the minute :sweat_smile:

I’m still having trouble understanding why my nested if to check the hasOwnProperty condition doesn’t seem to work to add the value to the end of the array. It seems to make sense to me based on the requirements of the question but I must be missing something obvious, would appreciate your inputs, thanks!

function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && value !== "") {
    records[id][prop] = value;
  } else if (prop === "tracks" && records[id][prop] === "") {
    records[id][prop] = [value];
  } else if (prop === "tracks" && value !== "") {
      if (records[id].hasOwnProperty(prop)) {
        records[id][prop].push(value);
      } 
  } else if (value === "") {
    delete records[id][prop];
  }

  return records;
}

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

What does this literally check?

1 Like

I’m thinking that it checks if there are any properties in the collection… but now i’m starting to realize that the question is specifically asking for if there are any “tracks” properties in the first place… thanks so much for the nudge Jeremy! lemme tweak things a bit!

Thank you Jeremy and Ilena! I realised I mixed up challenge statements 2 and 3 and I should have been checking for the “tracks” property availability in my second ifelse statement. All good now and I’m happily moving forward! :raised_hands:t5:

here you are doing again something similar

what is records[id][prop]? can it have a value of ""?

You don’t check if an object has a property with ===, with that you check if something is equal to something else

EDIT: oh, Jeremy already pointed that out

1 Like

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