Can't Figure Out record Collection Challenge

Tell us what’s happening:

So I am trying to do this my way, and not completely copy the solution code. Although, I can’t seem to make this code work. What I have makes sense to me, but something is not registering right, and I can’t figure it out. Help!

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 !== 'tracks' && value !== "") {
  object[id][prop] == value;
} else if (prop === 'tracks' && object[id][prop] !== 'tracks') {
  object[id][prop] == [];
  object[id][prop].push(value);
} else if (prop === 'tracks') {
  object[id][prop].push(value);
} else if (value === "") {
  delete object[id][prop];
}
console.log(collection);
return object;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0.

Challenge: Record Collection

Link to the challenge:

What do you want this line to do? Can you describe it in words?

Okay, i will give it a try.

If the prop is tracks and the collection object property does not have a tracks property do this…

I updated code based off of this question

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


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

// running tests
After updateRecords(collection, 5439, "artist", "ABBA"), artist should be ABBA
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, 2548, "tracks", ""), tracks should not be set
After updateRecords(collection, 1245, "albumTitle", "Riptide"), albumTitle should be Riptide
// tests completed

object[id][prop] is not an object. What’s the object you want to test?

1 Like

the collection object.

the collection object has specific strings as keys, those strings are numbers.

could tracks ever be one of those keys?

Maybe like this?

else if (prop === ‘tracks’ && collection.hasOwnProperty(‘tracks’) === false)

console.log(Object.keys(collection)) if I write this, which will print to the console the keys of the collection object I get [ '1245', '2468', '2548', '5439' ]
the collection object doesn’t have the tracks property by definition

Ah got it! Okay so collection[id].hasOwnProperty(‘tracks’), right?

Okay, so I reformatted to make it more clear and reading the instructions over again.

Just getting these two errors. :man_shrugging:t2:

// Only change code below this line
function updateRecords(collection, 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 {
    return collection[id][prop] = value;
  }
  return collection;
}

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

// running tests
After updateRecords(collection, 5439, "artist", "ABBA"), artist should be ABBA
After updateRecords(collection, 1245, "albumTitle", "Riptide"), albumTitle should be Riptide
// tests completed

why do you return this here?

1 Like