Record Collection Reading Error

Tell us what’s happening:
Hello, I am having a bit of trouble with this problem even after checking what other users posted. This is what I get when running the test:

Cannot read property ‘5439’ of undefined
Cannot read property ‘5439’ of undefined
After updateRecords(2548, “artist”, “”), artist should not be set
Cannot read property ‘1245’ of undefined
Cannot read property ‘2468’ of undefined
After updateRecords(2548, “tracks”, “”), tracks should not be set
Cannot read property ‘1245’ of undefined

I can’t figure out what I have formatted wrong to get a reading error. Also, How is the rest of the code?

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

  
  return collection;
}
}

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

Your browser information:

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

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

You several problems with your code.

  1. You closed your function off prematurely with } after the last else code block (see below)
    } else {
      collection[id][prop]=value;
    }
  1. You wrote your return collection line after the function was closed.

  2. You have an extra } at the end of your code.

  3. You should only have one else statement after an if. Only the first one will be executed. You can have else ifs after an if, but only one else. You have 3 else statements. What logic were you thinking would cause each of the other two to execute?

Check the matching brackets. The return is outside the function.

How I thought it was that the first “else” belongs to the innermost “if”, the second “else” was for the “if” with the hasOwnProperty condition and the last one for the outermost “if”.

You are correct. Because your code was not indented at the different levels, it was harder to see. Below is how the indentation would look to make it more readable:

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

	return collection;
}

Okay, I removed the "{"s that you pointed out at points 2. and 3. Now I get this:

updateRecords(…)[5439].tracks.pop is not a function
After updateRecords(2548, “artist”, “”), artist should not be set
updateRecords(…)[1245].tracks.pop is not a function
After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.
After updateRecords(2548, “tracks”, “”), tracks should not be set

I see. I’m still working on my formatting. Thank you.

No worries. I should have reformatted your code offline and I would have seen it before commenting.

Can you repost your latest code?

Sure, but it is still not formatted properly.


function updateRecords(id, prop, value) {
  if (collection[id][prop] == "tracks") {
    if (collection[id].hasOwnProperty("tracks")) {
      if (value!="") {
      collection[id][tracks].push(value);
    } else {
       delete collection[id][tracks];
    }
    
    } else {
      collection[id][tracks]=[];
      collection[id][tracks].push(value);
    }
    
      
    } else {
      collection[id][prop]=value;
    }


  
  return collection;
}

What do you think this is checking?

So that’s the culprit. Should I just replace it with " if (prop == “track”)? "
It seems to create errors as well.

tracks is not defined
After updateRecords(2548, “artist”, “”), artist should not be set
tracks is not defined
tracks is not defined
tracks is not defined

tracks is not a variable, so you can not use in the particular way you are trying reference it in the above lines. You can use either dot notation or use brackets, but inside the brackets, you must place tracks in quotes.

That narrowed it down. But there is still one thing, which apparently wasn’t fixed by adding if (value!="") at the end. Here is my code:

function updateRecords(id, prop, value) {
  if (prop == "tracks") {
    if (collection[id].hasOwnProperty("tracks")) {
      if (value!="") {
      collection[id]["tracks"].push(value);
    } else {
       delete collection[id]["tracks"];
    }
    
    } else {
      collection[id]["tracks"]=[];
      collection[id]["tracks"].push(value);
    }
    
      
    } else {if (value!="") {
      collection[id][prop]=value;
    }
    }
    


  
  return collection;
}

Here is the error:

After updateRecords(2548, “artist”, “”), artist should not be set

So how I understand it is that, despite the fact that value is “”, something still happens, even if it shouldn’t. Is that correct?

Try with !== instead of !=.

Your first if statement (seen below) checks if prop is equal to “tracks”.

if (prop == "tracks") {

In the case where id is equal to 2548 prop is “artist”, it is not equal to “tracks”, so you final else statement code block executes and then checks if value is not equal to a blank string. In this case it is a blank string, so the blank string is not assigned to collection[id][prop]. The problem is the instructions state:

If value is empty (""), delete the given prop property from the album.

Your code does not fulfill this instruction when the value is equal to a blank string.