Has anyone completed Basic JS Record Collection in a different way?

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

I check the Hint section, there are plenty of answers there, very old. They, however, gave me guidance on what I needed to do. Of my logic is flawed please let me know.

If value is an empty string, delete the given prop property from the album:

if (value === “”) {

  • delete collection[id][prop];*
  • }*

** If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .**

if (collection[id][prop] !== “tracks” && value !== “” ) {
value = collection[id][prop]
}

** If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.** (I’m struggling with this one)

if (collection[id][prop] === “tracks” || !collection[id].hasOwnProperty([prop])) {
collection[id][prop] = []
collection[id][prop].push(value)
}

If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.

if (collection[id][prop] === “tracks” && value !== “”) {
collection[id][prop].push(value)
}

return object;

I’m totally lost. FCC answer is completely different, although logical too. I even copied and pasted all other answers given by people in the Hint section, some others I found on google and none of them worked and I mean NONE of them worked!

What’s happening? Find it hard to believe there’s only one way to solve the problem.

The challenge was changed at one point so you have to make the adjustments needed (look at the function parameters). There are plenty of answers in the thread that works just fine after making the adjustment.

BTW, object[id][prop] === 'track' is not the same as prop === 'track' (or !== same thing).

1 Like

Yes, I can tell it was changed at some point.

I’m seriously stuck.

Just did this:

if (object[id][prop] !== "tracks" && value !=="") {
    object[id][prop] = value;
  }
  
  if (object[id][prop] === "tracks" && !object[id].hasOwnProperty([prop])) {
    object[id][prop]= [];
    }
  
  if (object[id][prop] === "tracks" && value !== "") {
    object[id][prop].push("tracks");
    }

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

And get 4 out of 7 right.

Also just changed object[id][prop] for prop and same, 4 out of 7.

This two:
** After updateRecords(collection, 1245, "tracks", "Addicted to Love") , tracks should have Addicted to Love as the last element.
**

** After updateRecords(collection, 1245, "albumTitle", "Riptide") , albumTitle should be Riptide**

I would suggest you log out object[id][prop] at the top of the function, I don’t think you are checking for what you believe you are.

I probably don’t mate. Understand you’re trying to help by giving me hints, but really super stuck here. Also, how can I log out object[id][prop] at the top? Because one of the hints FCC bot gives you is to access the object in that way?

If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
Is working with this

if (object[id][prop] !== "tracks" && value !== "") {
    object[id][prop] = value;
  }

If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.

Isn’t working with this

if (object[id][prop] === "tracks" && !object[id].hasOwnProperty(prop)) {
    object[id][prop] = [];
    object[id][prop].push(value);
  }

Thx mate.

Just put console.log(object[id][prop]) at the top of the function.

prop is a parameter, its value is just a string passed to the function (“artist”, “tracks”, “albumTitle”). You are looking at the object property object[id][prop].

For example for the function call:

updateRecords(collection, 2468, "tracks", "Free")

The value of object[id][prop] will be [ '1999', 'Little Red Corvette' ]

1 Like

Hey mate, you were very cryptic for how confused I was. At least I thought that. I can see now I was heading in the right direction -I believe- and you were just pushing bit by bit. That last answer helped, I was calling the total value of the prop on my If Conditional instead of the parameter whose value was changing.

Got really stuck on the array one. Just a quick question.

This didn’t work:

if (prop === "tracks" && !object[id].hasOwnProperty([prop])) {
    object[id][prop] = [].push(value);
    }

But this did:

if (prop === "tracks" && !object[id].hasOwnProperty([prop])) {
    object[id][prop] = [];
    }

That’s my confusion, why? Isn’t .push() supposed to add value to the newly created array and I need to put the parameter in the parentheses? Or is it a default setting when creating a new array because I’m already declaring prop===“tracks” as value in my operator?

This is my solution that worked:

if (prop !== "tracks" && value !== "") {
    object[id][prop] = value;
    }

    if (prop === "tracks" && !object[id].hasOwnProperty(prop)) {
    object[id][prop] = [];
    }

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

The push() method returns the new length of the array. If you are ever in doubt, just log out the code.

console.log([].push('test'))
// 1

And it’s always worth checking the MDN docs for the method to learn more.

1 Like