Help me, please. Lesson 'Record Collection'

Tell us what’s happening:

What do I need to change in my code to avoid errors? Can not understand.
This is problem:
After

updateRecords(collection, 5439, "tracks", "Take a Chance on Me")

,

tracks

should have

Take a Chance on Me

as the last element. After

updateRecords(collection, 1245, "tracks", "Addicted to Love")

,

tracks

should have

Addicted to Love

as the last element. After

updateRecords(collection, 2468, "tracks", "Free")

,

tracks

should have

1999

as the first element.
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(object, id, prop, value) {
if (prop !== 'traks' && value !== '') {
  object[id][prop] = value;
} else if (prop === 'traks' && !object[id].hasOwnProperty('traks')) {
  object[id][prop] = [];
} else if (prop === 'traks' && value !== '') {
  object[id][prop].push(value);
} else if (value === '') {
  delete object[id][prop];
}
return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');
updateRecords(collection, 5439, 'tracks', 'Take a Chance on Me');
updateRecords(collection, 2548, 'artist', '');
updateRecords(collection, 1245, 'tracks', 'Addicted to Love');
updateRecords(collection, 2468, 'tracks', 'Free');
updateRecords(collection, 2548, 'tracks', '');
updateRecords(collection, 1245, 'albumTitle', 'Riptide');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

the first issue I see is that it is tracks, not traks

Yes!
This will solve the issue

Thank you, but 1 point has not been resolved yet.

After

updateRecords(collection, 5439, "tracks", "Take a Chance on Me")

,

tracks

should have

Take a Chance on Me

as the last element.

My corrected code:

function updateRecords(object, id, prop, value) {

  if (prop !== 'tracks' && value !== '') {

    object[id][prop] = value;

  } else if (prop === 'tracks' && !object[id].hasOwnProperty('tracks')) {

    object[id][prop] = [];

  } else if (prop === 'tracks' && value !== '') {

    object[id][prop].push(value);

  } else if (value === '') {

    delete object[id][prop];

  }

  return object;

}

in that case you are creating an empty array with no value in it

if I put value, nothing changes

Try:

if (prop == “tracks” && !object[id].hasOwnProperty(“tracks”)){
object[id][prop]=[value];
}

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

You do not need if-else clauses, multiple if will do.

It is great that you solved the challenge, but instead of posting your 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 working solutions.

I am new to forums and helping people. without that code snippet I cannot explain it.
Can you help me explain and guide w/o the snippet ?

You do not need to give away the answer to help people to find the answer themselves.

In this case, I’d say


The instructions state

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

In this segment of your code

you have created the empty array, but you have not added the value to that array.

1 Like

Gottcha, thanks !
Make them learn to fish and not fish for them.

1 Like

Yep. Learning how to debug is hard, so I like to explain how I know what needs to be changed.

1 Like

lets wait to hear from olegashepelev if these breadcrumbs solves the mystery :grinning:

Thank you TheOwl and JeremyLT. I got it. :upside_down_face:

1 Like