Why does the property have to equal itself instead of the empty array

Tell us what’s happening:

Your code so far


// Setup
var collection = {
 2548: {
   album: "Slippery When Wet",
   artist: "Bon Jovi",
   tracks: [
     "Let It Rock",
     "You Give Love a Bad Name"
   ]
 },
 2468: {
   album: "1999",
   artist: "Prince",
   tracks: [
     "1999",
     "Little Red Corvette"
   ]
 },
 1245: {
   artist: "Robert Palmer",
   tracks: [ ]
 },
 5439: {
   album: "ABBA Gold"
 }
};

// Only change code below this line
function updateRecords(id, prop, value) {
 if(value===""){
   delete collection[id][prop];
 }

 else if(prop==="tracks"){
   collection[id][prop]=collection[id][prop] || [];
   collection[id][prop].push(value);

 }
 else{
   collection[id][prop]= value; 
 }
 

 return collection;
}

let songs =updateRecords(2468, "tracks", "love");
console.log(songs)

Your browser information:

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

Challenge: Record Collection

Link to the challenge:

the above code implies that if the album already a tracks property it will equal to itself and nothing changes(first line code)

but if the album does not have the tracks property then we add the tracks property and set it to an empty array (first line code)

after that we push the new value into the already existing array or the newly created empty array (second line code)

below is one of the sentences given in fcc challenge to do

If prop is "tracks" but the album doesn't have a "tracks" property, create an empty array before adding the new value to the album's corresponding property.
1 Like

@tejambsnks so we created a tracks property by equaling itself and at the same time added an empty array if the tracks property does not exist. So why use the or operator ?

if the property is not present then an empty array is created as a=a will non work becuse a does not exist(a=>property)

if a is present or operate will not work because the first equality will work a==a

check the output for the below codes 1 and 2

1
var a;
a=(a||[]);
console.log(a);
2
var a=[2,3];
a=(a||[]);
console.log(a);
1 Like

Okay, this makes since and thank you for the help ! @tejambsnks