Record Collection: first condition

I do not understand the why this does not work as the first condtion of my problem

function updateRecords(id, prop, value) {
let a=collection.id.album;
if (prop!="tracks"){
     if(value==""){
       value =a
     }
}


  return collection;
}

Link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection

I’m not entirely sure what you are trying to do with your if block, but you have a problem here: let a=collection.id.album;
Do you remember the difference between dot notation and bracket notation?

1 Like

I am not sure. I think brackets notation you use it when the name is seperated.

You should look at Accessing Object Properties with Dot Notation, Accessing Object Properties with Bracket Notation, and especially Accessing Object Properties with Variables.

1 Like

I am trying to accomplish the first condition in the problem

getting the difference between bracket notation and dot notation and how to use them correctly will bring you closer there.

what you are doing now is when the conditions are satisfied you are changing the value of the function parameter, you have not changed the object in any way

1 Like

Thanks! I could do the first condition and last one. I do not understand the reason why I could not accomplished the second and the third contidition. This is the fourth condition

} else if(prop=="tracks"&& value!=""){
    tracks.push(value);
  }

Third*************************************condition.

what’s tracks? a variable you have not declared anywhere.
And if you used "tracks"? well, that’s just a string
you can’t use push with a string or with undefined

You need instead to access an array (and you need correct usage of dot or bracket notation for this) and push there - you also need to remember that you can’t push to an array that doesn’t exist so you also need to check if the array exists or not

1 Like
var tracks=[collection[id]["tracks"]];
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 (prop!="tracks"&& value!=""){
    collection[id][prop]=value;
  } else if(value==""){
    delete collection[id][prop];
   
  } else if(prop=="tracks"&& value!=""){
   tracks.push(value);
  }
   


  return collection;
}

I declared it, but it is not working yet.

you can’t use variables before declaring them. At this point collection, id and prop are undefined.
You need to change approach, not try to define something that you don’t need anyway.

1 Like

I have to finish only a condition :(. I have to finish the second one. I set my code but it did not work it for fixing the problem

// 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 (prop!=="tracks"&& value!==""){
    collection[id][prop]=value;
  } else if(value===""){
    delete collection[id][prop];
  
  } else if(prop==="tracks"&& value!==""){
    collection[id]["tracks"].push(value);

} else if(prop==="tracks"&& "tracks"===undefined){
   var a=[];
   collection[id][tracks]=a;
}
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
console.log(collection[5439]["tracks"]);

I am trying to set the cose to accomplish the second condition, but I do not know hot add the new property, therefore to add “tracks” to the album and the var a = , whick was created recently. This is the link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection and this is my code

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 (prop!=="tracks"&& value!==""){
    collection[id][prop]=value;
  } else if(value===""){
    delete collection[id][prop];
  
  } else if(prop==="tracks"&& value!==""){
    collection[id]["tracks"].push(value);

} else if(prop==="tracks"&& "tracks"===undefined){
   var a=[];
   collection[5439]="tracks";
   
}
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
console.log(collection[5439]);

seconddddd connnditioooonnnnn

I applied push, but I has not passed the test :frowning:

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 (prop!=="tracks"&& value!==""){
    collection[id][prop]=value;
  } else if(value===""){
    delete collection[id][prop];
  
  } else if(prop==="tracks"&& value!==""){
    collection[id]["tracks"].push(value);

} else if(prop==="tracks"&& "tracks"===undefined){
  var a=[];
   collection[id][prop].push(a);
  
   
}
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
console.log(collection[5439]);

of these two conditions only the first one will execute, as only the first condition that is true will execute. You need to structure this part a bit differently, and check if the array already exist or bot before using push. Keep in mind that only the first true condition will execute!

1 Like

// 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 (prop!=="tracks"&& value!==""){
    collection[id][prop]=value;
  
  }else if(value===""){
    delete collection[id][prop];
  
  } else if(prop==="tracks"&& value!==""){
    collection[id]["tracks"].push(value);
 } else if(collection.hasOwnProperty("tracks")==false){
   var a = [  ];
   collection[id][prop]=tracks;
   tracks.push(a);
}
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
console.log(collection[5439]);

It is not working yet. I changed my second condition.

if the first condition is true, so if prop === "tracks" is true, that is the one executed. hasOwnProperty never does its check because there was a previous condition that was true

your second issue is that

var a = [];
collection[id][prop] = tracks;
tracks.push(a);

does not do what you want.
here you are creating an empty array, then setting collection[id][prop] to undefined (because tracks is an undefined variable), and then trying to push an empty array to undefined (this will give an error and stop your code from executing)

before all that you are using a variable that has not been defined, so that is also an error! tracks has never been defined with let, var o const

1 Like
I have done it! Thanks!
  
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 (prop !== "tracks" && value !== "") {
    
    collection[id][prop]=value;
  
  } else if (value === "") {
    
    delete collection[id][prop];
  
  } else if ( prop === "tracks" && value !== "") {
    
    if (collection[id]["tracks"] === undefined) {
    
      collection[id]["tracks"] = [];
    }
    collection[id]["tracks"].push(value);
 }
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
console.log(collection[5439]);

1 Like