Why is this solution wrong in Basic JavaScript: Record Collection?

I can see that I am not the only one who is having questions about Basic JavaScript: Record Collection. I worked out my solution in the Chrome Dev console and got the desired outcome based on the rules for the lesson on FCC. However, my solution is not getting a pass in FCC:


Here is my code:

It is producing an outcome that meets all the rules unless I am missing something big:

{ 1245: 
   { artist: 'Robert Palmer',
     tracks: [ 'Addicted to Love' ],
     album: 'Riptide' },
  2468: 
   { album: '1999',
     artist: 'Prince',
     tracks: [ '1999', 'Little Red Corvette', 'Free' ] },
  2548: 
   { album: 'Slippery When Wet',
     artist: 'Bon Jovi',
     tracks: [ 'Let It Rock', 'You Give Love a Bad Name' ] },
  5439: 
   { album: 'ABBA Gold',
     artist: 'ABBA',
     tracks: 'Take a Chance on Me' } }

I also found and ran the answer that FCC passes as the correct answer. I know because I pasted it in and it passes. However, it produced the following code with Bon Jovi the artist missing. I am really not getting it.

{ 1245: 
   { artist: 'Robert Palmer',
     tracks: [ 'Addicted to Love' ],
     album: 'Riptide' },
  2468: 
   { album: '1999',
     artist: 'Prince',
     tracks: [ '1999', 'Little Red Corvette', 'Free' ] },
  2548: 
   { album: 'Slippery When Wet',
     tracks: [ 'Let It Rock', 'You Give Love a Bad Name' ] },
  5439: 
   { album: 'ABBA Gold',
     artist: 'ABBA',
     tracks: [ 'Take a Chance on Me' ] } }

Anyone who would like to enlighten, please help! I would really appreciate it!

First, you don’t need the loop because you can just access the inner object with collection[id]

Plus, what do you think this is doing???
Do you know what hasOwnProperty() returns? why delete always the tracks property?

this is one, but seeing that you don’t have issues only with the ones with empty value there is something else for sure

Ah, here your issue with tracks:

if (!collection[id].hasOwnProperty(prop)) {
              // Create prop and add value
            collection[id][prop] = value;
            // prop is present in the item
          }

tracks should be an array, but if tracks is not present you are setting it to a string.

And instead if any other property is present but the value needs to be updated you are pushing to a string:

      else {
              // Add value to prop array
            collection[id][prop].push(value);
          }

Hi, ieahleen.
This is really helpful!

Plus, what do you think this is doing???
Do you know what hasOwnProperty() returns? why delete always the tracks property?
I updated it so that it is like this now:

https://repl.it/repls/BiodegradableStupidResearch

The same correct answer!!!
It has got to be something but you didn’t find it. Did you? I am not sure if I missed that one.
I spent a lot of time on what you pointed out “there is something else for sure”. You didn’t find what I missed. I am going to look for it. Hope someone else can help.
Thanks ieahleen! Much appreciated!

still not fixed.
Do you know what hasOwnProperty() returns?

also, seriusly, do it without the loop, it is not needed. You just make a lot of iterations, in most of which the else statement is executed. You anyway access things with collection[id], so you don’t need to check if id == key.

(and yes, I found the other issue, you posted just after my second post)

1 Like

See it now. I am going to fix it. Thanks so much!

Fixed the code. Now the tracks is an array. Thanks ieahleen’s comments. It performed all the tasks but is still not passing. Getting these error messages:

After updateRecords(2548, "artist", ""), artist should not be set

After updateRecords(2548, "tracks", ""), tracks should not be set

But the outcome of my code doesn’t show the artist and tracks of 2548 being reset:

=> { 1245: 
   { artist: 'Robert Palmer',
     tracks: [ 'Addicted to Love' ],
     album: 'Riptide' },
  2468: 
   { album: '1999',
     artist: 'Prince',
     tracks: [ '1999', 'Little Red Corvette', 'Free' ] },
  2548: 
   { album: 'Slippery When Wet',
     artist: 'Bon Jovi',
     tracks: [ 'Let It Rock', 'You Give Love a Bad Name' ] },
  5439: 
   { album: 'ABBA Gold',
     artist: 'ABBA',
     tracks: [ 'Take a Chance on Me' ] } }

It can very well be that I am not seeing something again. Any help is appreciated.
Here is the code:

One of the requirements of the challenge is:

If value is empty ( "" ), delete the given prop property from the album.

This means that updateRecords(2548, "artist", "") should result in the artist property of the object being deleted. Your solution does not remove the property. The test says “should not be set” not “reset to original object properties”.

Also, as @ilenia already said, there is no reason to iterate through the collection. The for loop just over complicates your code.

Thanks! I am going to give it a try.