Basic JavaScript: Record Collection Help!

Tell us what’s happening:
What’s wrong? I followed the video tutorial to the letter. Does it have something to do with the quote marks being different? I tried to account for that.

Your code so far


// 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(id, prop, value) {
if (value === ''){
  delete collection[id][prop];  
} else if (prop === 'tracks') {
  collection[id][prop] = collection[id][prop] || [];
  collection[id][prop].push(value);
} else {
  collection[id][prop] = value;
}

return collection;
}

//Alter values below to test your code
console.log(updateRecords(5439, 'artist', 'ABBA')



Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

Hello!

The solution still works, it’s just that the lesson was updated but the video hasn’t. You will need to copy your solution and reset the code, updating the solution accordingly.

The new test requires you to update the passed object instead of the collection that’s initially setup (this one is just for reference):

// 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) {
  // paste your solution here and replace collection with object

  return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');
2 Likes

Hi, first of.
I just started learning and I have no idea how comment works in fCC

getting to your problem, I believe @skaparate 's instruction is correct. To add on, your function updateRecords(id, prop, value) should be function updateRecords(object, id, prop, value)

hope this helps.

1 Like

Thanks for your help. I got my further with the code below. I’ve attached a screenshot.

// 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) {
  // paste your solution here and replace collection with object
if (value === ''){
  delete object[id][prop];  
} else if (prop === 'tracks') {
  object[id][prop] = object[id][prop] || [];
  onject[id][prop].push(value);
} else {
  object[id][prop] = value;
}

  return object;
}

updateRecords(object, 5439, 'artist', 'ABBA');
updateRecords(2468, ‘tracks’, ‘test’);

1 Like

You have a typo:

function updateRecords(object, id, prop, value) {
  // paste your solution here and replace collection with object
if (value === ''){
  delete object[id][prop];  
} else if (prop === 'tracks') {
  object[id][prop] = object[id][prop] || [];
  onject[id][prop].push(value); // <-- Here, 'oNject'
} else {
  object[id][prop] = value;
}

  return object;
}
1 Like

Thank you for your help. That’s a lot better. The code produced the results in the attached screenshot. Two of the outputs aren’t satisfied.

Do I have to make an entry for each element of the collection?

// 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) {
  // paste your solution here and replace collection with object
if (value === ' '){
  delete object[id][prop];  
} else if (prop === 'tracks') {
  object[id][prop] = object[id][prop] || [];
  object[id][prop].push(value);
} else {
  object[id][prop] = value;
}

  return object;
}
updateRecords(collection, 2468, 'tracks', 'test');
updateRecords(collection, 5439, 'artist', 'ABBA');

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Do I have to make an entry for each element of the collection?

No, you have to test if the property exists, otherwise create it so you can handle errors :slight_smile:. In this case, test that the array exists, otherwise initialize it.

Il Thank you. I’m new here and this problem is a bear.

So, it should be:

updateRecords(collection, 2468, 'tracks', 'test');
console.log(updateRecords(collection, 5439, 'artist', 'ABBA'))

Sorry about my previous answer, I was wrong :sweat_smile:!

I didn’t realize you changed this:

if (value === ' ')

You added a space, hence value === ' ' is false whenever you pass an empty value. Your initial solution was correct in that part:

if (value === '')
1 Like

Thank you. This worked. You were correct about changing collection to object. That was crucial.

1 Like