Record Collection vs Profile Lookup

Record Collection vs Profile Lookup
0.0 0

#1

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


#2

In the profile look up challenge, contacts is an array, so you can use a for loop to iterate through the elements. In the record collection, collection is an object which could be iterated through with a for in or for of loop, but it is not necessary to iterate through it to solve the challenge.


#3

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.


#4

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”);


#5

@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", ""));

#6

This is helpful. Thank you!


#7

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!