Record collection challange

so how do you put values at the end of the nested “track” array and/or delete the “track” prop?
or

You are doing it by .push()ing :wink:

Only in you case you’re .push()ing to an object: collection[ id[prop[value]] ].push("[]")

The correct syntax would be:

collection[id][prop].push(value);

For the test case updateRecords(2468, "tracks", "Free"); it would go like this:

id = 2468, prop = "tracks", value = "Free"

you are taking collection object, looking for a key 2468 (whose value is an object), then in that object, you are looking for the key "tracks" (whose value is an array). Now you are pointing to an array and you can .push() value to it.

2 Likes

Thank you :grinning:

I just wanted to say thanks for this post, it totally mirrors how I feel about some of the javascript challenges and it made me feel a lot better about how I’ve been struggling with them. I haven’t done any coding before FCC so it’s very reassuring to know that someone with experience is having the same problems.

My experience is that quite a few of the challenges pre-suppose knowledge that hasn’t been covered (at least not explicitly) in the preceding waypoints. It would be better if there were smaller challenges that built up to the harder ones (like Record Collection). Alternatively, if extra knowledge is required it would be good if that was made clear.

I particularly agree with your statement “I don’t feel like this type of challenge belongs that early in the curriculum with the knowledgebase that students have at that point.” For example the way to access and add a value to an id property (collection[id][prop] = value;) wasn’t specifically covered in previous waypoints. Maybe a lot of people could work it out from the info in different waypoints but I couldn’t.

I really like FCC and am grateful that it exists as an alternative to expensive online bootcamps and just trying to work out a track on your own, but there’s room for constructive criticism. It will hopefully help to improve the course for people in the future (I know it’s regularly updated) and also helps people not give up because they feel stupid when in fact they could work it out if they had all the required information.

8 Likes

Hey Jenovs! Thanks for your help on this forum. Please, I’d like you to explain how you came about the nested “if” statement in your “else if” statement. I was able to use your code to get past the challenge but I’m still yet to understand it what I did. I’d appreciate if you could assist me with a detailed explanation.

Regards

If you mean the code in the post Nr.4, that is not my code - I corrected OPs code and that code doesn’t work anymore - challenge has been slightly changed since then.

Could you post your working code and point out the part you have the problem with?

Thanks for your response boss.I stopped understanding from the “else if” which I’ve commented all the way to the end. Please, see my working code below. Thanks.

function updateRecords(id, prop, value) {
if (value !== ‘’ && prop !== ‘tracks’) {
collection[id][prop] = value;
/*
} else if (prop === ‘tracks’ && value !== ‘’) {
if (!collection[id].tracks) {
collection[id].tracks = []; // create empty tracks array if non exists
}
collection[id][prop].push(value);
} else if (value === ‘’) {
delete collection[id][prop];
}
*/

return collection;
}

In this challenge you go through requirements and implement them one by on.

So:


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

that’s your first if:

if (value !== '' && prop !== 'tracks') {
    collection[id][prop] = 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.

If prop is “tracks” and value isn’t empty (“”), push the value onto the end of the album’s existing tracks array.

Here are two condition - you must check if value is not empty and does the album has a property tracks in it.

So in outer if you check if you have any value to add to tracks and if there is value (value !== '') you go on and check if album has property tracks in it - that’s the second if (notice ! at the beginning - it means not, therefore second if reads “if no tracks in album”).

If second if is true (i.e. no tracks in album), you create a track property and assign an empty array to it (all in one step).

Last step you push value to the tracks array.

else if (prop === 'tracks' && value !== '') {
    if (!collection[id].tracks) {
      collection[id].tracks = []; // create empty tracks array if non exists
    }
    collection[id][prop].push(value);
  }

If value is empty (“”), delete the given prop property from the album.

last if statement:

else if (value === '') {
    delete collection[id][prop];
  }
9 Likes

I would love to hear a explanation from a expert as to why the following code doesn’t work.

Furthermore i finished the test but i think the explanation could have been better as i had to check the forums for some hints.

function updateRecords(id, prop, value) {
if (id.hasOwnProperty === true && prop.hasOwnProperty === true && value.hasOwnProperty === false) {
collection[id][prop] = ;
collection[id][prop].push(value);
}
else if (value === “”) {
delete collection[id][prop];
}
else if (id.hasOwnProperty === true && prop.hasOwnProperty === false) {
collection[id] = ;
collection[id].push(prop);
collection[id][prop] = ;
collection[id][prop].push(value);
}
else {
collection = ;
collection.push(id);
collection[id] = ;
collection[id].push(prop);
collection[id][prop] = ;
collection[id][prop].push(value);
}
return collection;
}

// Alter values below to test your code
updateRecords(5439, “artist”, “ABBA”);

The following is being shown in console provided by FCC.

collection =
[
Null
Null
and allot more Null

The idea behind my code is that you can add a id as well as a prop and value. I also have to mention that one of the requirements is full filled. The following

After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.

I hope someone can elaborate with me.

Thanks for breaking it down like you did. I’ve been doing OK with these challenges but this one seemed so loaded with instructions that I was overwhelmed. I wasn’t able to complete it on my own. The second else if statement is where I got stuck but now after seeing the code and how it works I think I’m getting it… :+1:

1 Like

This helped a lot, thanks.

This helped me too. Thanks!
I have been pulling my hair all day today. :persevere:

This is what I did,

 function updateRecords(id, prop, value) {
   if (value === "") {
     delete collection[id][prop];
     return collection;
   }

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

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

   return collection;
  }
2 Likes

Thank you for your anwser it works good

Here’s what worked for me:


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

return collection;
}

1 Like

I think, this stores the given value to the prop given in the id given in the collection.
For example:

updateRecords(5439, “tracks”, “Take a Chance on Me”);

collection[id][prop] = value; will store "Take a Chance on Me" on tracks property of the id 5439.

Can Someone Pleae walk me through this and explain your process to discovering your answer. I spent roughly 3 hours researching and assessing this challenge before I gave up and started just looking for the answers. The first answers that I went to (Rafase282 FCC Wiki) and when I entered the code, it still had (After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.) as “X” but after researching and discovering this forum I was eventually able to solve the challenge. I understood the hints that were given in the instructions and I implemented to my best ability but I just got the placements in the if/else statements wrong and some other minor novice mistakes…

Thanks for explaining it a way that makes sense.

This is an answer I am finding easier to digest - some of the others go way over my head!

Can you please tell me if this is correct:

function updateRecords(id, prop, value) {
if (value === “”) {
delete collection[id][prop];
return collection;
}

The above means that if value is missing, you delete that particular property entirely and then return collection.

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

The above means that if you have a property called ‘tracks’ do this:

  1. if there is no property called tracks, you first make an array and give it the property called tracks
  2. you push a new value to that newly created array

I don’t get what the else part of the statement does :expressionless:

Thanks!

2 Likes

You are correct chad.

When I copied and pasted the code, it didn’t do the proper indentation for some reason so that might be why the else statement is not very clear which if statement it belongs to:
The else statement is in regard to the:
if (prop == “tracks”) {

So if the prop is album or artist, values of these properties will be updated.

Hope it helps.
Cheers!