Record collection (super confused)

Tell us what’s happening:
Describe your issue in detail here.
I was having a very hard time with this challenge, so I decided to walk through the steps of solution 1 and I still have so many questions.
When we are checking if prop is tracks, why don’t we use bracket or dot notation methods to access the prop as we did in earlier challenges and why are we putting tracks in quotation marks?
Also, can we use the return command after every if/else if statement and if not, why are we putting it at the end of the code?
My last question is where did even records come from? The variable containing the objects is named recordCollection and it was what I was putting at the beginning to access the object (i.e. recordCollection[id][prop]).
Is it normal that I got completely lost on this assignment considering that this is my first time learning Java?
Your code so far


// Setup
var recordCollection = {
2548: {
  albumTitle: 'Slippery When Wet',
  artist: 'Bon Jovi',
  tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
  albumTitle: '1999',
  artist: 'Prince',
  tracks: ['1999', 'Little Red Corvette']
},
1245: {
  artist: 'Robert Palmer',
  tracks: []
},
5439: {
  albumTitle: 'ABBA Gold'
}
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop !== "tracks" && value !== "") {
  records[id][prop] = value;
}
else if (prop === "tracks" && records[id].hasOwnProperty("tracks")=== false) {
  records[id][prop] = [value];
}
else if (prop === "tracks" && value !== "") {
  records[id][prop].push(value);
}
else if (value === "") {
  delete records[id][prop];
}
return records;
}

updateRecords(recordCollection, 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/92.0.4515.107 Safari/537.36

Challenge: Record Collection

Link to the challenge:

I can answer these questions, but I’d very much rather try helping you fix what code you had before you copied the solution.

1 Like

Hi @NKos !

I added spoiler tags around your code since this is a working solution.


Here is why I think so many beginners struggle with record collection.
And why I also struggled with it a year ago. :grinning:

Up until this point, most of the challenges can be solved in a few lines of code.
There are a couple like “counting cards” and “golf code” that require more lines of code but most of them are pretty short.

Then we get to record collection.

This is the first big algorithm challenge you have to tackle.
Your task is to break down each of the rules in the instructions and write out a set of conditions.
You are being asked to review previous lessons just to pass this challenge.

But remember what programming is really about.
It is not just about learning languages.

It is about problem solving.

Languages are just tools to help you solve the problem.

My advice would be to revisit this problem when you have forgotten the solution and try the challenge again.
But this time, slowly code out each condition and come to the forum with specific questions you have pertaining to your code.

Reading other people’s code is a good skill to have.
But learning how to problem solve and create your own solutions is important too. :grinning:

Plus, learning how to tackle record collection and understanding it is a good warmup for the algorithm sections later on.

Hope that helps!

2 Likes
function updateRecords(records, id, prop, value) {

 if (recordCollection[id][prop] !== tracks && value !== "") {

   return recordCollection[id][prop] = value ;

 }

 else if (recordCollection[id][prop] === tracks)

 else if (recordCollection[id][prop] === tracks && value !== "") {

   return recordCollection[id][prop].push(value);

 }

 else if (value == "") {

   delete recordCollection[id][prop];

 }

 

}

this is what I had coded first but the second statement is incomplete since i was not sure how to use hasOwnProperty here.

I will answer this question though :grinning:

recordCollection is the global variable.
That just happens to be the object we are testing in this function call here

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

But your function should work for any object.
Not just recordCollection.

The records parameter represents any musical album collection.
That is why you have to reference records and not recordCollection.

Hope that makes sense!

1 Like

I agree that this is a lot more challenging compared to earlier sections since this one requries a more complex and layered code. It is good to know that I am not the only one struggling:)) Thank you for your advice, I will definitely come back at this challenge and retry it without looking at the hints.

1 Like

so this code would work if we had another variable containing other different album collections?

Yes that is correct.

Our goal is to write reusable functions that can work with any object literal, not just recordCollection.

1 Like

The way hasOwnProperty works is that it returns a boolean value.
Either the property exists or not.
It’s either true or false.

We have to write code that says if album doesn’t have a tracks property

It might help to look at the documentation examples.

You can also use the Logical Not operator in your answer if you feel comfortable with it.

1 Like

note that Java and Javascript are two really different languages

1 Like

Basically what you are asking about is called “Early return pattern” and yes, you can do that. This approach could be better in some situations. Let’s try to rewrite this function with it (keeping same functionality):

function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && value !== "") {
    records[id][prop] = value;
    return records;
  }
  
  if (prop === "tracks" && records[id].hasOwnProperty("tracks")=== false) {
    records[id][prop] = [value];
    return records;
  }
  
  if (prop === "tracks" && value !== "") {
    records[id][prop].push(value);
    return records;
  }

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

  return records;
}

As you can see above using this pattern for current situation is not the best idea because it adds repetitiveness which breaks DRY principle.

To sum up, any pattern has it’s own up- and downsides and should be used mindfully.

For more detailed explanation please look here:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.