Record Collection problems

when i run tthe program it says
After updateRecords(5439, “artist”, “ABBA”), artist should be “ABBA”

Cannot set property ‘tracks’ of undefined

Cannot set property ‘artist’ of undefined

Cannot read property ‘tracks’ of undefined

Cannot read property ‘tracks’ of undefined

Cannot convert undefined or null to object

After updateRecords(1245, “album”, “Riptide”), album should be “Riptide”

the code is:

function updateRecords(id, prop, value) {
if(prop!=“tracks” && collection[id].hasOwnProperty(prop)==true){
collection.id[prop]=value;
console.log(collection.id[prop]);
}
else if(prop==“tracks” && collection[id].hasOwnProperty(prop)==false){
collection.id[prop] = [value];

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

collection.id.tracks.push(value);

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

return collection;
}

// Alter values below to test your code
updateRecords(5439, “tcks”, “ABBA”);


**Your browser information:**

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

**Link to the challenge:**
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection

Use bracket notation for accessing object’s properties if the properties are variables / parameters.

The same thing applies here.
If you fix these errors, you will have another problem - the structure of if-else statements. Read the rules of the problem and try to respect their order.

1 Like

Thank you for the responce!I dont see what the problem is…
I changed it a bit my new outcome is:
// running tests
After updateRecords(5439, “artist”, “ABBA”), artist should be “ABBA”
tracks is not defined
tracks is not defined
After updateRecords(1245, “album”, “Riptide”), album should be “Riptide”
// tests completed

AND my new code is:

function updateRecords(id, prop, value) {
if(prop!=“tracks” ){
if( value!="" && collection[id].hasOwnProperty(prop)==true){
collection[id][prop]=value;
console.log(collection[id][prop]);
}

  else if(value=="" && collection[id].hasOwnProperty(prop)==true ){
   delete collection[id][prop]; 
  }

}
else if(prop==“tracks”){
if(collection[id].hasOwnProperty(prop)==false && value!="" ){
collection[id][prop] = [value];}

      else if(value!="" && collection[id].hasOwnProperty(prop)==true){

collection[id][tracks].push(value);}

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

}
return collection;
}

Please paste your code inside of ``` tags, like so:

```
… your code here …
```

I added some comments in your code to clarify what is happening.
1: At first you are testing if the prop != tracks. (So any artist or album input would run through here. This runs with Abba.)
1a: Then you test if the value is not blank, and the collection has property. (Since artist doesn’t exist for 5439, this won’t run.)
1b: Then you test if value is not blank, and collection has property. (Again, since artist doesn’t exist for 5439, this won’t run.)
-At this point, your if statement is finished. Abba didn’t run 1A or 1B, so it returns the collection as is.

// 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"
	}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
	if (prop != "tracks") {
		console.log("1 Prop is not a track.")
		if (value != "" && collection[id].hasOwnProperty(prop) == true) {
			console.log("1a Track - Value is not blank & collection has property.")
			collection[id][prop] = value;
			console.log(collection[id][prop]);
		} else if (value == "" && collection[id].hasOwnProperty(prop) == true) {
			console.log("1b Track - Value is blank & collection has property")
			delete collection[id][prop];
		}
	} else if (prop == "tracks") {
		console.log("2 Prop is tracks.")
		if (collection[id].hasOwnProperty(prop) == false && value != "") {
			console.log("2a Value is not blank & collection has property.")
			collection[id][prop] = [value];
		} else if (value != "" && collection[id].hasOwnProperty(prop) == true) {
			console.log("2b Value is not blank & collection has property")
      collection[id][tracks].push(value);
		} else if (value == "") {
      console.log("3 Value is blank.")
			delete collection[id][prop];
		}
	}

	console.log(collection[5439]["artist"])
	return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Try to think through it logically. It’s tough at first but gets easier with practice. What I ended up doing for this lesson:

-1: Test If value exists. Now it will run 1A or 1B.

-1A: Test If props does not equal tracks. This would be an album or an artist.
–Then you can save the value into collection[id][prop].

-1B: Test If props equals tracks. This is just a track.
-1B1: Test if collection[id] does not have the property of prop. If there is no property in the collection, we need to create one. Save an empty value into collection[id][prop] so that it exists.
-1B (again): The collection either exists, or was just created. So you can push the value onto the end of collection[id][prop].

-2: Else, we know value doesn’t exist. The user tried to add a blank entry, so we can delete the collection[id][prop].

1 Like

thank you so much for your help !!!