Record Collection

Hi i am stuck on Record Collection lesson. I read the hint saying I should create an array before push the value. I am not sure how to create a new array first. and can someone explain why i have to use “===” instead of “=” prior to false for

records[id].hasOwnProperty(‘tracks’) = false

“=” will create an error.

please help. here is my full code:

// Setup
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’
}
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop != “tracks” && value != “”) {
records[id][prop]=value;
}
else if (prop = “tracks” && records[id].hasOwnProperty(‘tracks’) = false){
records[id][prop].push(value);
}
else if (prop = “tracks” && value != “”){
records[id][prop].push(value);
}
else if (value = “”){
delete records[id][prop];
}

return records;
}

updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’);

What does = do? What does === do?

You can’t push on an array that doesn’t exist. How did you make arrays in previous lessons?

1 Like

= is not one of comparison operators. it only used when giving value to variables. == is equality. when checking the two operands, it tempts to convert the types with two are not the same. === is strict equality. it’s only true if both value and type are the same. Thanks. now the definitions are more clear to me after looking it up.

I made array like this:

const arr = [1, 2, 3];

i tried the following but it’s not working:

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

if (prop !== “tracks” && value !== “”) {
records[id][prop]=value;
}
else if (prop === “tracks” && records[id].hasOwnProperty(‘tracks’) === false){
records[id][prop]; //create array prop
records[id][prop].push(value); //push value
}
else if (prop === “tracks” && value !== “”){
records[id][prop].push(value);
}
else if (value === “”){
delete records[id][prop];
}

return records;
}

This does not create an array. This only references records object property with the same name as prop that is located in the album object (whose property name is the same as id). You have not assigned a value to it. You just referenced it.

In the if statement above this else if code block, you actually assigned the same property the value of the value parameter.

Got it! finally understand.
thank you so much!!

1 Like