Basic JavaScript: Record Collection cannot understand

Basic JavaScript: Record Collection cannot understand
0

#1

Hi, I’ve been stuck in this code challenge for several days. I have read all the posts about it and I didn’t get anything. The explanation given in the challenge is not very enlightening. This is my code right now and with this I don’t get any green check.

function updateRecords(id, prop, value) {
  if (prop === 'tracks' && collection[id]['tracks'] === undefined) {
    collection.id['tracks'] = []; 
  } else if (prop != 'tracks' && value != '') {
    collection.id[prop].push(value);
  } else if (value === '') {
    delete id.prop;
  }
  
  return collection;
}

Thanks a lot.


#2

You need to check for the value being passed. An empty value means you are expected to delete a property. Add a value checker to your first if-condition:
if (value && prop === 'tracks' && collection[id]['tracks'] === undefined)

When you want to use a variable reference an object property, you MUST use the bracket notation. Change all the occurences of collection.id[prop] to collection[id][prop]. Also, since only the property tracks is an array, just set the properties with the = operator. Don’t use the Array method .push.

} else if (prop != 'tracks' && value != '') {
   collection[id][prop] = value; 
} 

Fix your delete statement.

else if (value === '') {
    delete collection[id][prop];
}

If value is not “”, then your must add the song to the album, even when it has no album:

if (value && prop === 'tracks' && collection[id]['tracks'] === undefined) {
    collection[id]['tracks'] = [];
    collection[id][prop].push(value);
}

Now you are almost in business :slight_smile:
What to do when it does have an album and value is not empty? This condition is missing.


#3

Hi thanks for your answer! I changed my code based on what you told me and now I get some green checks but I still don’t get the logic. This is my code now:

function updateRecords(id, prop, value) {
  if (value && prop === 'tracks' && collection[id]['tracks'] === undefined) {
    collection[id]['tracks'] = [];
    collection[id][prop].push(value);
  } else if (prop != 'tracks' && value != '') {
   collection[id][prop] = value; 
  } else if (value === '') {
    delete collection[id][prop];
  }
  return collection;
}

In the first IF statement, how can value and prop be both ‘tracks’?

And in the second one why you do collection[id][prop] = value?


#4

These are actually two different expressions. If value is a blank string, then it would be a “falsy” value which would render then entire if statement as false, so as long as value has anything other than an empty string and the other two conditions evaluate to true, then the if statement evaluates to true also.

Writing:

value && prop === 'tracks'

is like writing

value !== '' && prop === 'tracks'

#5

Oh ok I get that part now, I was a little confused…
With the code of my last post I only get this error:

After updateRecords(1245, "tracks", "Addicted to Love"), tracks should have "Addicted to Love" as the last element.

But that is done with the first IF isn’t it?


#6

No. Because in this case, tracks is not an empty array. The last condition in your if-statement makes the whole block false.
collection[id]['tracks'] === undefined
Therefore, it does not run the first statement for that case.


#7

I finally got it done!! I don’t know why but this if else exercise has been the most difficult for me to understand.
Thanks a lot!