Record Collection Challenge Help

Record Collection Challenge Help
0

#1

I have been stuck on the Record Collection challenge for at least an hour and a half or so. I even broke down and read the “solution” in the Wiki, which doesn’t seem to account for all of the conditions we’re supposed to test.

I feel like I understand how to perform each individual task outside of the conditionals. But what I can’t get right is the code.

This is the latest version of my code:

if (value !== '' && prop !== 'tracks') { collection[id].prop = value; } else if (value!== '' && prop === 'tracks') { collection[id][prop].push(value); } else if (!collection[id].tracks) { collection[id].tracks = []; collection[id].tracks.push(value); } else if (value === '') { delete collection[id][prop]; }

This passes all but the first two conditions in the challenge. Placing (collection[id].prop = value;) outside of the “if” block causes the first condition to pass but all others to fail.

Given what FCC has gone through so far on the Map, what should be my next steps?


#2

Hey! This actually tricked me a bit too. Look below if you want the solution because you really aren’t far off it. It’s similar to yours, just in your first if statement use bracket notation to access prop instead of dot notation and then to check if the tracks property is already there just create an if statement within the first else if.

  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];
  }

#3

Thanks! I just had the chance to go over this tonight. So, there were a couple of issues. First, my syntax, and second, the order of my logic.

I feel like there should be a more concise way to code what I came up with (and maybe even more concise than using a nested conditional), but for reference in case anyone else has trouble with this challenge in the future, this was what I came up with:

(EDIT: can’t get the preformatted text to work tonight, sorry!)

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

if (!collection[id].tracks) {
collection[id].tracks = [];
collection[id].tracks.push(value);
} else if (value!== ‘’ && prop === ‘tracks’) {
collection[id][prop].push(value);
}

if (value === ‘’) {
delete collection[id][prop];
}`


#4

You’re thinking like a programmer! Red, green, refactor! Keep it up.

  • a trick I learned from Simon Allardice is to write down on paper my algorithm before even opening the editor. Also, when stuck for too long, stop and go for a walk or something. Happy coding

#5

This code works for me:

function updateRecords(id, prop, value) {
if (value === “”) {
delete collection[id][prop];
}
else if (prop === “tracks”) {
if (!collection[id].tracks) {
collection[id].tracks = []; // create an empty tracks array
collection[id][prop].push(value);// fill empty array with value
}
else{
collection[id][prop].push(value);
}
}
else {
collection[id][prop] = value;
}
return collection;
}


#6

Hello friends. I need help. I keep looking, but I can’t figure out why my code doesn’t work. Problem is with fifth test: After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element. My result is:
“tracks”: [
“free” ]
instead: “tracks”: [
“1999”,
“Little Red Corvette” ,
“free”
]
This is my code:

function updateRecords(id, prop, value) {
if(prop!==“tracks” && value !==’’){
collection [id][prop]=value;
}

else if(prop==“tracks” && value!==""){
if(!collection.hasOwnProperty(“tracks”)){
collection[id][prop]=[];

}

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

}
else if (value===’’){
delete collection[id][prop];
}

return collection;
}

// Alter values below to test your code
updateRecords(2468, “tracks”, “Free”);


#7

if(!collection.hasOwnProperty(“tracks”)) { // this line has error
collection[id][prop]=[];
}
instead of this you can write
if(!collection[id].hasOwnProperty(“tracks”)) { // we missed on which particular id’s " tracks" we are updating
collection[id][prop]=[];
}