Basic JavaScript - Record Collection

Can anyone kindly tell me what is wrong with my code and why it’s not working?

if (object[prop] == “”) { delete object[prop]};
else if (object.hasOwnProperty(“tracks”) == false) {
object[tracks] = “”
};
else if (object.hasOwnProperty(“tracks”)) {object[tracks].push(value)};
else {object[prop] = value};

1 Like

Please post your full code and a link to the challenge. Thanks

1 Like

Challenge link : https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection

Full code :

// 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 (object[prop] == "") { delete object[prop]};
  else if (object.hasOwnProperty("tracks") == false) {
  object[tracks] = ""
  };
  else if (object.hasOwnProperty("tracks")) {object[tracks].push(value)};
  else {object[prop] = value};
  return records;
}

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

What is object?

What’s this doing? An empty string isn’t an array

It would really help if you use conventional formatting/indentation.

1 Like

The error I am getting is " SyntaxError: unknown: Unexpected token (25:2)". I think if I can fix this all should be fine.

1 Like

You have more problems than that. Can you try to answer my questions?

1 Like

Silly mistake. Updated the code but still have problem at 25:2

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

updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’);

1 Like

Hmm, you are closer, but you want individual albums in the records object

1 Like

What else is left ?

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


updateRecords(recordCollection, 5439, 'artist', 'ABBA');= []
  };
1 Like

It would really, really help if you used conventional indentation. Your sticking the entire if condition and body on the same line is masking a syntax problem.

1 Like
  • If value is an empty string, delete the given prop property from the album.
  • If prop isn’t tracks and value isn’t an empty string, assign the value to that album’s prop.
  • 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.

read the condition

  1. if value is empty
  2. else if prop is tracks
    1. if .hasOwnProperty(“tracks”), then push
  3. else assign the value to that album’s prop.;

follow this approach you will find your answer

I don’t know what else I can do. !

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

  else if (records[id].hasOwnProperty(tracks) != true) 
   { records[id][tracks] = [] };

  else if (records[id].hasOwnProperty(tracks))
   { records[id][tracks].push(value)};

  else { records[id][prop] = value};

  return records;
}

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

Put the } separate from the body. No examples in the curriculum show the indentation you’re using.

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

Also note, you should use === not ==

Do the instructions want you to make a totally empty array and do nothing to it? no, they don’t. Read that but carefully because it’s tricky

here is the correct code

Mod Edit: SOLUTION REMOVED

I don’t think those && operators are necessary. I wrote the code myself . You can check it out

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.