Record collection challange

Record collection challange
0

#22

I found this exercise strange,… I was reading through the code in an attempt to understand it, and then I thought "hmm I’ll just run it first to see what it does ‘as is’ " So I did, and it passed ‘as is’, I did not change a thing, I wondered it this was intentional, just to get us to read through and understand it… Then I wondered if I had already done it the night before and just forgot, LOL. Nope I really don’t remember doing this one.

Anyone else run into this?

Update: I opened this challenge in a browser that I was not signed in to FCC, and I see the problem written out without the solution. So, for some reason, where I was signed on in Chrome, the solution was already given to me for some reason. Very strange, I KNOW I did not write that solution…


#23

Which online tool you have been using? I remember using something like flex environment for step-by-step debugging, however I do not remember how it was set up :blush:


#24

I just did this it took me a few ones for a few reasons

  1. Nested Ifs - they’re annoying - and there are better ways to do it be writing functions that would be called - but gotta give the answer they want
  2. dot notation versus array notation - why wasn’t collection.id recognized but collection [id] was - is that because they represented a number as a string?
  3. The last issue was after you’ve solved for tracks - if you do it step by step the way i did - you have to remember that there’s a special thing to do for ‘empty value’ which you have to incorporate into your tracks solution.

There are more elegant solutions that could be written (write an update tracks function separately - if the property passed is tracks, call that function with id and value as arguments.) but at least I got through it :slight_smile:

I don’t really think you need a separate debug tool - the course editor seems to be some sort of sublime text version that tells you errors and even suggestions - plus the tests on the left will tell you where you’ve faile


#25

Because in collection.id case it would look literally for “id” thing. But your id is actually a variable which is passed to the function (in function updateRecords(id, prop, value)).


#26

but id isn’t defined anywhere else - shouldn’t local scope MAKE id equal the passed id argument. Within the scope of the function id should be equal to the passed value - shouldn’t it?


#27

Dear FCC,

This challenge needs to be refactored. I myself spent way too long on it, I get asked about it all the time in the chat room, and then there are forum posts like this…

A difficult challenge is one thing, but I find that most people who struggle with it, as did I, simply don’t understand what it’s asking/don’t have enough training to understand it. That means the challenge or curriculum could be improved.


#28

I disagree - it’s relatively straight forward and just requires you to read ALL the instructions and think it through properly - I’m not sure what a long time is - but the only issue I had after 5 minutes or so was the blank/tracks thing and that was my own issue of not clearly reading the instructions. The failed tests are right there for you so you can see where you’re failing.

It’s only slightly more complicated than the standard fizzbuzz problem.

I agree that it should be refactored - but not due to difficulty - i think it’d be cool if the ‘tracks’ was supposed to be a separate function - thus recalling something learned earlier - my only pet peeve so far is that there are very few ‘call backs’ in lessons to show us how things fit together.


#29

Yes, of course, id is local and its value is equal to whatever was passed to the function. Thing is, with dot notation you are checking not with id’s value, but with string “id” (and there are no property named “id”, so it cant find it).


#30

Ok - i think i get it now - coming from a ruby background sometimes my brain gets confounded - i keep forgetting to write return due to the fact that ruby has no real use for it if it’s the last line of the function (something I think all OO languages should pick up)


#31

@jemagee

I don’t know what to tell you, I simply don’t find the challenge text to be as well written as it should be sometimes… I’m not going to re-write the challenge but I can say that it could definitely be re-written to be more clear. It is obvious that the challenges were written by a coder!

One may argue that as a coder, we will not always be given super clear instructions, and the instructions we’re given may not be in “coder language” like the challenge is at least. I agree.

However, I don’t feel like this type of challenge belongs that early in the curriculum with the knowledgebase that students have at that point.

I spent a disproportionately large amount of time on that challenge (and the other object data access challenge) compared to others, and while that doesn’t bother me that much, I feel it could turn people off to coding at that early of a level.

I read a 1,000 page textbook cover-to-cover (working through the exercises as well) and took a course on the C# programming language, built a web browser, a system diagnostic application, an inventory system hooked up with social media logins, an SQL database, and a data model, and am currently working on public safety dispatch software and in the course of all of that, I seriously hadn’t run into anything so tough to understand as a couple of these (record collection/profile lookup) challenges. To me, combined with the fact that I’ve seen loads and loads of people in chat get hung up on these, combined with forum posts like this which have a ton of views, tells me that the challenge or curriculum surrounding it could be improved.

On the contrary, one could argue that the benefit to freeCodeCamp in fact comes from the community, so challenges that send students running to the forum are actually a good and beneficial aspect of the learning curriculum because ultimately, working and interfacing with others is perhaps one of the most important career steps. I would agree with this viewpoint; i am open-minded.

Either way, I really like freeCodeCamp and am having a blast. The community is great, the projects are fun. But not everyone is physically able to put in the countless hours coding while learning like I am in my current situation so I just don’t want people to be unnecessarily put-off by a poorly written or poorly placed challenge.


#32

I didn’t read all of this, because I don’t want the spoiler, but I think this is relevant, since I’m working on this challenge. My question is basically the following:

My code seems to work when I use bracket notation, but not dot notation. Does collection.id.prop !== collection[id][prop]? Am I not able to use dot notation to access nested object properties? If so, why not?


#33

collection doesn’t have a property id that’s why you can’t use dot notation (id is a variable passed to function updateRecords() not a property of collection).


#34

Thanks a lot @JohnL3. I found your explanation about the syntax in the if(!collection…) really useful andI could find my way out of this challenge. I really appreciate your help.

@djnanas I think the dot notation doesn’t handle strings ( when the variable change to it’s value it fail), that’s why we have to use the brackets notation. If I am wrong someone correct me please. :wink:

Let keep rocking.

//SPOILER ALERT
My working code:

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

  } else {
    delete collection[id][prop];
  }
  
  return collection;
}

#35

there is also an error in

if (!collection.hasOwnProperty("tracks")) {

it should be

if (!collection[id].hasOwnProperty("tracks")) {


#36

The challenge has been changed since my answer.


#37

Why is the exclamation necessary on the (!collection[id].hasOwnProperty(“tracks”)). ? I have not seen any lesson refer to this practice. If you know one please tell me. This is the first lesson that was so hard I needed full blown help with it. So I will be studying it over until I understand the code line by line!!


#38

The exclamation mark means ‘not’, so it flips the truthy test.

It’s equivalent to writing:

if (collection[id].hasOwnProperty("tracks") === false) ...

#39

@JacksonBates Thanks bro, I was just chatting with someone about the same thing. It flips the boolean value. We have not done this much with variables inside functions. I would have never thought to do it on my own! I searched the answer too the lesson before I knew it, LoL :smiley:


#40

Hello room. Chat not wokring properly, so I am here on the reocrd collection challenge and I am stuck. Does anyone mind critiquing my code:

// 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 (prop !== “tracks” && value !== “”){
var newartist = collection[id[prop[value]]];
var chgartist = collection[id[prop[value]]];
console.log(newartist);
} else if (prop == “tracks” && “tracks” === null){
var newprop = collection[id[prop[value]]];
} else if (prop == “tracks” && value !== “”){
var newvalue = collection[id[prop[value]]].push("[]");

} else if (value === “”) {
collection[id[prop[value]]].pop();
}
return collection;


#41
  1. collection is an object and objects don’t have .push() and .pop() methods

  2. You are using bracket notation incorrectly.
    Yours:
    collection[id[prop[value]]]
    Correct:
    collection[id][prop][value]