Populating an object by a function

Populating an object by a function
0

#1

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


#2
  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 }