Record Collection Challenge for Beginner

Record Collection Challenge for Beginner
0

#1

Before you start to read my question please understand I am a very confused beginner.

I can’t even seem to get this one off the ground and I’ve been working on it on and off for days. I am really trying super hard to not give up and look at the answer!

Starting with the very basics…

I see that we are supposed to write a code that uses ‘id’ , ‘prop’ and ‘value’ as variables.

From the challenge: Write a function which takes an album’s id (like 2548 ), a property prop (like "artist" or "tracks" ), and a value (like "Addicted to Love" ) to modify the data in this collection.

Correct me if I’m wrong:
The ‘id’ is the four digit number that represents the record in the collection…Got it.
I understand the ‘prop’ variable to be the properties of the objects, the bits that sit on the left side of the semicolon in the contents of objects. I understand the ‘value’ variable to be the values that the individual properties are referring to, the bits on the right side of the semicolon.

ex. var Object = {
“property1” : “value1”,
"property2 ": “value2”
}

Now my question is how does the code know that when i use the variable ‘id’ I am referring to the four digit code for each album? Do I have to write code that explicitly tells the computer that ‘id’ is equal to each object in var collection? Or is this some type of hard wired code in Javascript and everytime I use the variable ‘Id’ in a function where I am manipulating Objects it already knows that ‘Id’ is the variable that refers to the name of the Objects.

I am assuming before I write any if/else if statements I need to let the code know what I am talking about when I use ‘Id’, ‘prop’, and ‘value’, in any subsequent code.

I understand that: collection[0] = “2548” , collection[1] = “2468”, collection[2] = “1245”, collection[3] = “5439”…

But how do I write code that sets ‘id’ equal to the objects in collection?

I am thinking something like this:

var x = 0;
var id = collection[x];
(code that tests the individual album according to the problem)
x ++; (increment to next object)

I realize this code above really doesn’t make sense and would require some type of loop but we haven’t been taught loops up to this point. You see what I am trying to do I’m sure… test the first album, then the next, then the next in succession.

Am I even in the ball park here?

Super confused guys…


#2

First, I’m assuming you’re talking about this challenge: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection/

Javascript doesn’t know nothing about the variable id.

I understand that: collection[0] = “2548” , collection[1] = “2468”, collection[2] = “1245”, collection[3] = “5439”…

This is just wrong, collection (in this challenge) is not an array, it is an object. The id is just the property name, exactly what you’ve said about the left side of the colon. The right side of the colon is the property value, which could be anything like a string or a number, but it happens to be another object.

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" 
      ]
    },
...

You can’t do collection[0], as I’ve said this is not an array, you must do collection["2548"].

function updateRecords(id, prop, value) {...}

updateRecords(5439, "artist", "ABBA");

As you can see, the FCC code calls the function and give us an id argument. Which means we can access it like this collection[id] and it will return the value (right side of colon), which is an object. If I want to access the property FCC code give us, I’ll do this collection[id][prop]. Note that there’s no difference between id and prop, there’s nothing special in these names, you could call then arg1 and arg2 and the functionality would be the same.

Suppose I want to change the artist in the collection with the if of “2468”. I’d like this:

collection["2468"]["artist"] = "Michael Jackson"

Of course I can use the variables instead of hard-coding those values.

Hope this helps.


#3

Yes this is the challenge I am referring to, sorry I should have clarified.

I see what you are saying actually! Thanks so much!!

So if I understand you correctly you are saying that … collection[0] would access the first piece of data in collection if it were an array. But its not an array its an Object. So for example to access the “Bon Jovi” value in the collection i’d have to do something like this: collection[“2548”][“artist”].

Awesome

I am still a bit confused as to how I write code to tell the computer that when i use the variable ‘id’ i am referring to all the record id numbers.

Is it just by virtue of the order in which you write the code collection[id][prop]? For example if I began writing code as collection[prop][id], I am assuming the computer would understand the variable ‘prop’ to be referring to the record number id’s now right?

From the challenge:

If prop isn’t "tracks" and value isn’t empty ( "" ), update or set the value for that record album’s property.

I interpreted this in code as:

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

I understand this to mean that if the object has a property other than “tracks” (i.e “artist” or “album”) and the value for this property isnt empty (i.e “artist” : “Bon Jovi”), then ‘update or set the value for that record album’s property.’…

I am a bit confused as to what… ‘update or set the value for that record album’s property.’… means?

If “artist” : “Bon Jovi” then there wouldn’t be anything to update right? Which means I would have to ‘… set the ‘value’ for that record album’s property’ then right? Which means what?.. just return the entire collection object??

On a related note i feel like this challenge is beyond my skills at this point. I’ve been doing all the challenges in succession and have basically hit a wall with this one. Would I be better off just skipping this? I haven’t really made any progress in weeks.


#4

Exacly! If prop is “tracks” and id is “2548”, the computer would look for collection["tracks"]["2548"] and wouldn’t find anything.

This is wrong. The challenge is not asking anything about the collection yet, it just wants to know if the variable prop isn’t “tracks” and the value isn’t empty. (""). If not, then set the value to the album’s property.

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

I didn’t understand this. The challenge is asking to update (in case the value already exist) or set the value (in case the value doesn’t exist).

In any case, you do the same:

collection[id][prop] = value;

I think you shouldn’t skip it, but also you don’t need to keep using only FCC. I’d recommend you to search for tutorials or courses to complement what you are learning here. And keep asking on the forums, people here are very supportive.

Also don’t be afraid to search google, even experienced programmers search google all the time, no way anyone can remember everything in programming. What makes someone good at programming is the ability to search google efficiently.

Just to put you on the right track, this challenge is like a cake recipe. Read each line (requirement) and think on how to put it on your code.

Requirements:

  1. If prop isn’t "tracks" and value isn’t empty ( "" ), update or set the value for that record album’s property.
  2. 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.
  3. If prop is "tracks" and value isn’t empty ( "" ), push the value onto the end of the album’s existing tracks array.
  4. If value is empty ( "" ), delete the given prop property from the album.

Every requirement start with an if, so your function will probably have 4 if's. The first one we did it above.

Let’s do the second one, step by step:

  1. 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.
// If  `prop` is  `"tracks"`
if (prop === "tracks") {} 

// ------------------------------

// but the album doesn't have a  `"tracks"` property,
// we check if the object has a property with `hasOwnProperty`
if (prop === "tracks" && collection[id].hasOwnProperty(prop) !== true) {}

// ------------------------------

// create an empty array before adding the new value to the album's corresponding property.
if (prop === "tracks" && collection[id].hasOwnProperty(prop) !== true) {
  collection[id][prop] = [];
  collection[id][prop].push(value);
  // since we know `prop` is "track"
  // it is the same if we write `collection[id]["track"]`
}

#5

Ghukahr, thanks a lot man you have been very generous with your explanations.

I followed your list of requirements, turned them into if statements and passed the challenge.

But i’m not quite sure what I’ve really done here.

When I execute this code and display collection (console.log(collection)), I get the exact same original collection object. Wasn’t this code supposed to update the collection?

Or was this code only supposed to enable updateRecords(5439, “artist”, “ABBA”) to add the “artist” property, along with the “ABBA” value to album id 5439?

And if thats the case couldnt I have just done this:

collection[“5439”][“artist”] = “ABBA”;


#6

This line at the end of your code updateRecords(5439, "artist", "ABBA"); is just to show you how the FCC tests will call your function. Go ahead and delete it, your code will still pass the tests.

What’s happening is that FCC, behind the scenes, is calling your function 7 times:

52

Yes. You’re right that you could’ve done this, but that’s why there’s more than one test. So your function handles a lot of scenarios, not only that one.

Glad you passed the challenge :+1: