Build a Record Collection - Build a Record Collection

Tell us what’s happening:

I have attempted to get a check on message three a few different ways. no matter how I slice it I keep getting an X.

Your code so far

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'
  }
};

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

  if (value === '') {
    delete records[id][prop]
  }
  else if (prop !== "tracks" && value !== '') {
    records[id][prop] = value
  }
  else if (prop === "tracks" && value !== "") {
    records[id].tracks.push(value)
  }
  else if (prop === 'tracks' && value !== '' && record[id].hasOwnProperty('tracks') === false) {
    records[id][tracks] = value
  }
  return records
}

Your browser information:

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

Challenge Information:

Build a Record Collection - Build a Record Collection

What happens when you test your app?

I get a type error.

TypeError: Cannot read properties of undefined (reading 'push')

This is what I get when I try to log message 3 in the console.

it looks like this is the only line where you use push

you have these two else if, do you think the second one is ever going to run? if prop === 'tracks' && value !== '' is true the first condition is true and the second one is not checked, if it’s false then also the second one is false

I see, so I should swap the order in which these are run? That way it gives the last statement a chance to validate? I’ll give that a shot and see if it works.

Nope lol, now Messages 3, 5, and 6 are all failing.

what is your code now?

if it’s just the one you have but with the two if else swapped, consider, do you have a tracks variable?

and afterthat, what is the value needed for the tracks property?

also yes, what okdvalis says, test your app
add the function calls mentioned in the failed tests, look at what’s happening

Use console.log() to confirm the variables and that the logic works the way you expect (if the expressions evaluate to true or false)

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

  if (value === '') {
    delete records[id][prop]
  }
  else if (prop !== "tracks" && value !== '') {
    records[id][prop] = value
  }
  else if (prop === "tracks" && value !== '' && Object.hasOwn(record.id,"tracks") === false) {
    records[id][prop] = value
  }
  else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value)
  }
  
  return records

This is my code as at stands currently, I will test the app and see what it returns.

and now everything fails with this message error in the console.

TypeError: Cannot convert undefined or null to object

I think I just need to attack message 3 from a different angle.

Well I’ve come at the problem from a different angle. I am using the undefined operator to validate the statement. The console logs what the message is looking for but it still is not passing the test. See current code below.

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

  if (value === '') {
    delete records[id][prop]
  }
  else if (prop !== "tracks" && value !== '') {
    records[id][prop] = value
  }
  else if (prop === "tracks" && value !== '' && records[id][prop] === undefined) {
    records[id][prop] = value
  }
  else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value)
  }
  return records
}

console.log(updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"))

Here is what is logged in the console after attempting to validate message 3.

// tests completed
// console output
{ '1245': { artist: 'Robert Palmer', tracks: [] },
  '2468': 
   { albumTitle: '1999',
     artist: 'Prince',
     tracks: [ '1999', 'Little Red Corvette' ] },
  '2548': 
   { albumTitle: 'Slippery When Wet',
     artist: 'Bon Jovi',
     tracks: [ 'Let It Rock', 'You Give Love a Bad Name' ] },
  '5439': { albumTitle: 'ABBA Gold', tracks: 'Take a Chance on Me' } }

Figured it out. I needed to create the value as an array and not a string.

Thank you both for your help and your input. Very much appreciated.

1 Like

good jo! debugging is a really useful skill to develop

Remember to use console.log inside the function so you can see what’s happening in there

    console.log(prop, prop === "tracks", value, value !== "") 
if (value === '') {
    delete records[id][prop]
  }
 else if (prop === "tracks" && value !== "") {
    records[id].tracks.push(value)
  }
  else if (prop === 'tracks' && value !== '' && record[id].hasOwnProperty('tracks') === false) {
    records[id][tracks] = value
  }