# Populating an object by a function

**URL:** <https://forum.freecodecamp.org/t/populating-an-object-by-a-function/48314>\
**Category:** General\
**Created:** [October 24, 2016, 12:17pm UTC](https://forum.freecodecamp.org/t/populating-an-object-by-a-function/48314 "2016-10-24T12:17:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lisarko8077](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lisarko8077/32/21708_2.png) [@lisarko8077](https://forum.freecodecamp.org/u/lisarko8077)\
**Post date:** [October 24, 2016, 12:17pm UTC](https://forum.freecodecamp.org/t/populating-an-object-by-a-function/48314/1 "2016-10-24T12:17:06Z")

</div>

Hello,

i’m doing the exercises Record collection, but it goes wrong. could you help me?

` var collection = {  
2 “2548”: {  
3 “album”: “Slippery When Wet”,  
4 “artist”: “Bon Jovi”,  
5 “tracks”: [  
6 “Let It Rock”,  
7 "You Give Love a Bad Name"  
8 ]  
9 },  
10 “2468”: {  
11 “album”: “1999”,  
12 “artist”: “Prince”,  
13 “tracks”: [  
14 “1999”,  
15 "Little Red Corvette"  
16 ]  
17 },  
18 “1245”: {  
19 “artist”: “Robert Palmer”,  
20 “tracks”: []  
21 },  
22 “5439”: {  
23 “album”: "ABBA Gold"  
24 }  
25 };  
26 // Keep a copy of the collection for tests  
27 var collectionCopy = JSON.parse(JSON.stringify(collection));  
28  
29 // Only change code below this line  
30 function updateRecords(id, prop, value) {  
31  
32 if ((prop === “tracks”) && !(id.hasOwnProperty(prop))) {  
33 collection[id][prop] = [];  
34 collection[id][prop].push(value);  
35 }  
36 else if ((prop === “tracks”) && (value === “”)){  
37 collection[id][prop].push(value);  
38 }  
39 else if (value === “”) {  
40 delete collection[id][prop];  
41 }  
42  
43 collection[id].push(prop);  
44  
45 return collection;  
46 }  
47  
48 // Alter values below to test your code  
49 updateRecords(5439, “artist”, “ABBA”);``

---

<div class="post-metadata">

**Author:** ![Quickz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/quickz/32/105441_2.png) [@Quickz](https://forum.freecodecamp.org/u/Quickz)\
**Post date:** [October 24, 2016, 2:11pm UTC](https://forum.freecodecamp.org/t/populating-an-object-by-a-function/48314/2 "2016-10-24T14:11:45Z")

</div>

1. You should check whether “value” is empty first.
2. “hasOwnProperty” is used on the wrong variable. You have to tell whos id it is.
3. The line above return statement is executing every time(it shouldn’t). Also the “value” has to be assigned to the right property instead of getting pushed there.

## Marked spots that need a fix.

30 function updateRecords(id, prop, value) {  
31  
32 if ((prop === “tracks”) && **!(id.hasOwnProperty(prop))**) {  
33 collection[id][prop] = [];  
34 collection[id][prop].push(value);  
35 }  
36 else if ((prop === “tracks”) **&& (value === “”)**){  
37 collection[id][prop].push(value);  
38 }  
39 else **if (value === “”) { // Needs to be moved**  
40 **delete collection[id][prop];**  
41 **}**  
42  
43 **collection[id].push(prop);**  
44  
45 return collection;  
46 }

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 5:30am UTC](https://forum.freecodecamp.org/t/populating-an-object-by-a-function/48314/3 "2021-01-16T05:30:54Z")

</div>


