Basic JavaScript: Record Collection HELP

Hello all. I am in need of some help and understanding of what this problem is asking. I have taken each criteria and evaluated and then put it into what I believe is the right statement.

// Only change code below this line
function updateRecords(id, prop, value) {

//If value is empty (""), delete the given prop property from the album.
if (value === "") {
  delete collection[id][prop];
}
 //If prop is "tracks" and value isn't empty (""), push the value onto the end of the album's existing tracks array.
else if (prop == "tracks" && value != "") {
  collection[id][prop].push(value);
}
//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.
else if (prop == "tracks" && collection[id].hasOwnProperty(tracks) == false) {
  collection[id][prop] = collection[id][prop][""];
}

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

  return collection;
}


console.log(updateRecords(5439, "tracks", "test"))

The function works for certain things and not others. Of course it doesn’t pass. Here is an error I keep getting:

TypeError: Cannot read property 'push' of undefined

This has been kicking my butt for a week. I had stopped last week and decided to go learn Javascript through other means and then over the last day I have watched the 3 hour video for FCC on Youtube and stopped at this point. I have seen the answer on that video but really want to understand what it is about what I am trying to do here.

Please help me understand each of these if statements I have made and why they are incorrect.

Thank you!

First, you’re creating empty array in the wrong way. Remember how we usually assign a variable to an empty array, this is no difference–you’re assigning an empty array [] as a value to a property within the object.

Also you’re assigning the same thing to itself collection[id][prop][value] = collection[id][prop][value];, you should be setting the value of a property to the passed value parameter of the function.

In short, your last two conditions are buggy.

Adding up to previous reply, I don’t see any return value, and I guess it should be the collection object.

Edit

I’ll give you a couple of hints, but there is a way to go yet…


function updateRecords(id, prop, value) {
if (value === "") {
  delete collection[id][prop];
  return collection
}
else if (prop == "tracks" && value != "") {
  collection[id][prop].push(value);
return collection;
}
else if (prop == "tracks" && collection[id].
hasOwnProperty(tracks) == false) {
  let newArray =   [] 
  newArray.push(value)
  collection[id][tracks]=newArray
  //collection[id][prop] = collection[id][prop][""];
}
else {
  collection[id][prop] = value
  return collection;
}
}
updateRecords(5439, "artist", "ABBA");

Suggestion
The code of high-level languages is already verbose, use little comments. Of course, when we start comments are helpful, but at some point, if you feel more confident, write less comments.

what happens here if the tracks array doesn’t exist? well, you get an error, because you can’t push to an array that doesn’t exist

you need to make sure the array exist before pushing

@selectiveduplicate your second comment makes complete sense. That’s something I wasn’t comprehending. As for the first statement I just don’t exactly get it still. I think what you said directly relates to what @anon10002461 posted with the let newArray and if that’s the case then I could do it that way. I did end up using the format from the video and I’m guessing it accomplished the same thing but I don’t know why

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

@ILM Once you posted that it hit me as to why I kep getting that error only on the one id that didn’t already have the ‘tracks’ property created. I ended up moving my statement from the following else if and putting it right before the .push. That fixed the problem and allowed me to delete the next else if.

The test finally passed with this code

if (value === "") {
  delete collection[id][prop];
}else if (prop == "tracks" && value != "") {
  collection[id][prop] = collection[id][prop] || [];
  collection[id][prop].push(value);
}else {
  collection[id][prop] = value;
}
  return collection;
}

So my final questions are how is
collection[id][prop] = collection[id][prop] || [] the same as assigning a blank array to a variable.

Would I need a return on every step of the function? (doesn’t seem so)

Why did the guy in the youtube video not use && for any of his conditions in the if and else if's? Is the way I did it unnecessary?

Thanks again!

1 Like

There is something called truthy and falsy values in js (google search it).

  • If you write a || b, b only executed when a is falsy. In this case, if a does not exist or is empty (that’s falsy), you set an empty array.
  • If you write a && b, b is only executed when a is truthy

Otherwise nothing happens (I could be wrong here)

But I’d say, open up a terminal or a console and try it yourself, sure you will understand it.


Now as a side kick, write only code you understand, it doesn’t matter if it’s 1 or 5 lines now…


Edit

As for the second part, you don’t need a return value, that’s correct, the object is mutated, and the function will only be useful for that particular object.

But I just quickly read the exercise and didn’t think much

Your function must always return the entire collection object.

1 Like

you can just add the return statement as last line of the function

1 Like