Record Collection vs Profile Lookup

Hi, I am struggling with the Record Collection and Profile Lookup challenges. Can someone explain why you don’t use a for loop for the Record Collection challenge but you do for the Profile Lookup challenge? Any insight would be greatly appreciated, thanks!

Cheers,
Carie

Thanks for the response! So with the profile look up challenge it is because we are actually iterating through an array. I feel like I could really use some more practice with objects and going through challenges like this in order to solidify my understanding of it.

I don’t feel like there was enough explanation before this lesson. There was a lot to lookup for me including the delete operator (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete). See my answer below:

// Only change code below this line
function updateRecords(id, prop, value) {

if (value === “”) {

delete collection[id][prop]

}

else if (prop === “tracks”) {
collection[id].tracks = collection[id].tracks || []
collection[id].tracks.push(value)

}

else {
collection[id][prop] = value

}
return collection;
}

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

1 Like

@cariepigeon It was more technical than challenging. To be sincere, i have worked with the delete option before. This was how i understood that i was to use a delete function in the first place. So i support your suggestion on adding a little more context for the sake of people who have issues with this problem.

below is my own solution,

//Only change code below this line
function updateRecords(id, prop, value) {
    if (collection[id].hasOwnProperty(prop) === false) {//property doesnt exist
        if (prop === "tracks") {//if tracks, create and array
            collection[id][prop] = [];
            collection[id][prop].push(value); //populate the created array with the value
        } else collection[id][prop] = value; //else, just create the property and assign the value
    } 
    else if (collection[id].hasOwnProperty(prop) === true) {//property already exist
        if (value == "") { //no value to add
            delete collection[id][prop]; //delete the exisiting property
        } else if (prop === "tracks" && value !== "") {//else push values to the tracks array
            collection[id][prop].push(value);
        } else if (value !== "") {//just assign the value if it is not the property is not === tracks
            collection[id][prop] = value;
        }
    }

    return collection;
}
//console.log(updateRecords(2548, "tracks", ""));
1 Like

This is helpful. Thank you!

Hi,
That was nice of you to share that solution.

Since it is a working solution could you wrap it in spoiler tags so people still working on the lesson don’t accidentally see it?

[spoiler]
This text will be blurred until clicked
[/spoiler]

Thank you!

1 Like

// Only change code below this line
function updateRecords(id, prop, value){
if(collection[id].hasOwnProperty(prop) && value !== “”){
collection[id][prop].push(value)
} else if(!collection[id].hasOwnProperty(prop) && (prop === “tracks”) && (value !== “”)){
collection[id][prop] = []
collection[id][prop].push(value)
} else if(!collection[id].hasOwnProperty(prop) && (prop === ‘artist’) &&(value !=="")){
collection[id][prop] = value
} else if(!collection[id].hasOwnProperty(prop) && (prop === ‘album’) && (value !== “”)){
collection[id][prop] = value
}

 return collection

}[quote=“chinonsoebere, post:5, topic:195348, full:true”]
@cariepigeon It was more technical than challenging. To be sincere, i have worked with the delete option before. This was how i understood that i was to use a delete function in the first place. So i support your suggestion on adding a little more context for the sake of people who have issues with this problem.

below is my own solution,

//Only change code below this line
function updateRecords(id, prop, value) {
    if (collection[id].hasOwnProperty(prop) === false) {//property doesnt exist
        if (prop === "tracks") {//if tracks, create and array
            collection[id][prop] = [];
            collection[id][prop].push(value); //populate the created array with the value
        } else collection[id][prop] = value; //else, just create the property and assign the value
    } 
    else if (collection[id].hasOwnProperty(prop) === true) {//property already exist
        if (value == "") { //no value to add
            delete collection[id][prop]; //delete the exisiting property
        } else if (prop === "tracks" && value !== "") {//else push values to the tracks array
            collection[id][prop].push(value);
        } else if (value !== "") {//just assign the value if it is not the property is not === tracks
            collection[id][prop] = value;
        }
    }

    return collection;
}
//console.log(updateRecords(2548, "tracks", ""));

[/quote]

Can anyone help to check what went wrong with my logic!!

This solution seems to work and is better than the one currently offered on the Wiki, I believe.

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