Freecodecamp: Checkpoint: Record Collection

Created on 6 Jan 2016  路  10Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge Checkpoint: Record Collection has an issue
Write a function which takes an id, a property (prop), and a value.

For the given id in collection:

If value is non-blank (value !== ""), then update or set the value for the prop.

If the prop is "tracks" and value is non-blank, push the value onto the end of the tracks array.

If value is blank, delete that prop.

Always return the entire collection object.
My code:

// Setup
var collection = {
    2548: {
      album: "Slippery When Wet",
      artist: "Bon Jovi",
      tracks: [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    2468: {
      album: "1999",
      artist: "Prince",
      tracks: [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    1245: {
      artist: "Robert Palmer",
      tracks: [ ]
    },
    5439: {
      album: "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function update(id, prop, value) {
if(value !== " "){
  if(prop == "album" || prop == "artist" )
  collection.id[prop]=value;
  else if(prop == "tracks"){ 
  collection.id.prop.push(value);
  }}
 else delete collection.id.prop;


  return collection;
}

// Alter values below to test your code
update(5439, "artist", "ABBA");


Most helpful comment

I updated this to work with later versions of this problem:

This would go in the function.

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

  return collection;

All 10 comments

Issue does not describe a problem. Thanks and happy coding!

// Setup
var collection = {
2548: {
album: "Slippery When Wet",
artist: "Bon Jovi",
tracks: [
"Let It Rock",
"You Give Love a Bad Name"
]
},
2468: {
album: "1999",
artist: "Prince",
tracks: [
"1999",
"Little Red Corvette"
]
},
1245: {
artist: "Robert Palmer",
tracks: [ ]
},
5439: {
album: "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function update(id, prop, value) {
if(value !== "" && prop !== "tracks") {
collection.id[prop] = value;
} else if (prop === "tracks" && value !== "") {
collection.id[prop].push(value);
} else if(value === "") {
delete collection[prop];
}

return collection;
}

// Alter values below to test your code
update(5439, "artist", "ABBA");

The Error:
TypeError: Cannot set property 'artist' of undefined

I think even if the 'artist' is undefined it should add a property with name 'artist' and value as passed above.

The problem is that id is not a property of collection. id is a variable that contains the name of a property of collection. You must use bracket notation to access it. IE collection[id]

@santosh7227
@SaintPeter
thank you very much

my code after solving errors:

function update(id, prop, value) {
if(value !== ""){
if(prop == "album" || prop == "artist" )
collection[id][prop]=value;
else if(prop == "tracks"){
collection[id][prop].push(value);
}}
else delete collection[id][prop];

return collection;
}

// Setup
var collection = {
    2548: {
      album: "Slippery When Wet",
      artist: "Bon Jovi",
      tracks: [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    2468: {
      album: "1999",
      artist: "Prince",
      tracks: [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    1245: {
      artist: "Robert Palmer",
      tracks: [ ]
    },
    5439: {
      album: "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

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

  var objId=id; 
 var objProp=prop;
  if (value!==""&& prop!=="tracks")

{
    collection[objId][objProp]=value;
  }
  if (value!==""&&prop==="tracks")
{
  //var objprop=prop;
  collection[objId][objProp].push(value);
}

  if (value===""){

    delete collection[objId][objProp];
  }
  return collection;
}

// Alter values below to test your code
update(5439, "artist", "ABBA");

I wanted to know whether my approach is correct for the above algorithm

also
after many hit and trail i got the combination collection[objId][objProp] (successful approach)

why did other approach did not lead me to the answer like

collection.objId.objProp
collection[objId].objProp

and other combinations ?

also what is the significance of

var collectionCopy = JSON.parse(JSON.stringify(collection));

and the return collection

Sorry, but Issues are not for code questions. Please ask for help in HelpJavascript Chat or CodeReview Chat.

Thanks and Happy Coding!

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

}
// Alter values below to test your code
updateRecords(5439, "tracks", "Take a chance");

I updated this to work with later versions of this problem:

This would go in the function.

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

  return collection;

Thanks Aaron, very nice solution. I've been very confused by the wording of that particular challenge. Particularly this bit: "If prop does not contain the key "tracks", then update or set the value for that incomplete prop."
I was wondering, if the property does not exist, how can you give a value to it? I think I need to understand what "value" means in this context. I understand it means give the missing prop a name/value which is tracks which is encapsulated in:

else if (prop !== 'tracks') {
collection[id][prop] = value;

Please correct me if I'm wrong.

aarontaylor, you can make it even simpler :+1:

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

  return collection;
Was this page helpful?
0 / 5 - 0 ratings