Record Collection - almost there

Hi there:

I’m almost done with the exercise, but something isn’t quite right with my answer regarding the first else if where it says that records doesn’t have a tracks property. I used the hasOwnProperty method, because it, I assume, will come back as false. Then I am supposed to create an empty array. I don’t know if I’m doing it correctly.

Screen Shot 2022-01-03 at 2.33.05 PM

Please provide you code instead of a picture of your code. Thanks.


The entire records object will never have a tracks property.

Also, you shouldn’t create an empty array just floating around. The purpose of this function is to modify the records object.

1 Like

Can you provide the link to the exercise too, if possible?

1 Like
function updateRecords(records, id, prop, value) {
  if(prop !== 'tracks' && value !== '') {
    records[id][prop] = value
  } else if(prop === 'tracks' && records.hasOwnProperty('tracks')) {
    let array = []
    records = array
    array.unshift(value)
  } else if(prop === 'tracks' && value !== '') {
    records[id][prop].push(value)
  } else if(value === '') {
    delete records[id][prop]
  };
  
  return records;
}

Thanks. Did you see my comments above?


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 (’).

This can never be true.

You don’t want to create an orphaned array. This function should modify the records object. (Also, syntax highlighting is suggesting [correctly] that array is a bad variable name)

You definitely don’t want to clobber the entire records object.

Cheers, I’m gonna work on it.

Let us know if you have more questions!

Sure, sorry.

I think I got the hasOwnProperty method to equal a boolen false. However, I’m still stuck on how to create an empty array: I’ve done research to find out what an orphan and empty array are, tried multiple solutions but nichts.

// function updateRecords(records, id, prop, value) {
  if(prop !== 'tracks' && value !== '') {
    records[id][prop] = value
  } else if(prop === 'tracks' && records[id].hasOwnProperty('tracks') === false) {
     
     

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

Here you are modifying the value of records[id][prop]

You are doing it here too…

So you should probably do that here too. This: records[id].hasOwnProperty('tracks') === false means that the records[id]["tracks"] doesn’t exist though, so the ‘create an empty’ array is talking about creating a value for records[id]["tracks"] before you try to add the value for this new track.

If you are still stuck, I suggest retrying the lesson.
I used to have the same problem as you did. I couldnt find a fully correct solution to this one. After some time retrying , I was able to arrive at the solution.
If you already tried again by resetting but you’re still not able to get it, try again!
Now you know a lot more about this topic than what you knew when you started. It should help. Good luck!!

1 Like

Go back to the Project Specification:

Specification simplified:

  1. !prop is ‘tracks’ && !value is empty : update that property
  2. prop is ‘tracks’:
    a. !records.id has ‘tracks’ : add tracks to records, then push value to tracks
    b. records[id] has ‘tracks’ : push value to tracks
  3. value is empty : delete the prop

BTW. recordCollection is an “object” and is not an array

Programming attitude should be: if you are exhausted, dont bang your keyboard, but take a break or sleep for a while.

2 Likes

Hello again: I think I understand the idea here, but I haven’t been able to execute it. This is what I’ve got:

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

this will just replace the entire data for that specific ‘id’
meaning the structure, data type maybe change
and all the other key-value maybe gone.

Object Keys Tutorial

Programming attitude should be: if you are exhausted, dont bang your keyboard, but take a break or sleep for a while

The two most valuable things in IT roadblocks are, in no particular order, REM cycles, and a second set of eyes!

This kind of tags on to some other comments, but I think refactoring without the use of && may lead to the solution. That is not to say you cannot use them, but just that it may simplify the logic and flow of things.

Never be afraid to write things simply. You can refactor and make more elegant later, if time and asks permit :wink:

2 Likes

What I also found to be very helpful is to write down the instructions you’re finding hard to comprehend and simplify them.

1 Like