Record collection code

Tell us what’s happening:

what’s wrong with my code please?
Your code so far


// Setup
var collection = {
 2548: {
   albumTitle: 'Slippery When Wet',
   artist: 'Bon Jovi',
   tracks: ['Let It Rock', 'You Give Love a Bad Name']
 },
 2468: {
   albumTitle: '1999',
   artist: 'Prince',
   tracks: ['1999', 'Little Red Corvette']
 },
 1245: {
   artist: 'Robert Palmer',
   tracks: []
 },
 5439: {
   albumTitle: 'ABBA Gold'
 }
};

// Only change code below this line
function updateRecords(object, id, prop, value) {
 var resId =object.hasOwnProperty(id);
  var resProp =object.hasOwnProperty(prop);
 if (resId == true){
   if (prop != "tracks" && value !="") {
   object[id].prop= value;}

else  if (prop == "tracks" && resProp == false){
   var array=[];
   array.push(value);
   object.prop= array;}
else   if (prop == "tracks" && value !=""){
   object.id.prop.push(value);}
else   if (value == "" && prop !="") {
     delete object[id].prop;
   }

 } 
 return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

Remember you have nested objects here.

The following line:

var resProp =object.hasOwnProperty(prop);

will always be false for this object, because the prop values are never the values 2548, 2468, 1245, or 5439. However, object[id].hasOwnProperty(prop) will be true sometimes.

1 Like

Tell us what’s happening:

all the code is working correctly except adding a property at the end of the object,
any ideas?
Your code so far


// Setup
var collection = {
 2548: {
   albumTitle: 'Slippery When Wet',
   artist: 'Bon Jovi',
   tracks: ['Let It Rock', 'You Give Love a Bad Name']
 },
 2468: {
   albumTitle: '1999',
   artist: 'Prince',
   tracks: ['1999', 'Little Red Corvette']
 },
 1245: {
   artist: 'Robert Palmer',
   tracks: []
 },
 5439: {
   albumTitle: 'ABBA Gold'
 }
};

// Only change code below this line
function updateRecords(object, id, prop, value) {
  var resId = object.hasOwnProperty(id);
  var resProp = object[id].hasOwnProperty(prop);
  if (resId == true) {
    if (prop != "tracks" && value != "") {
      object[id][prop] = value;
    } else if (prop == "tracks" && value != "") {
      object[id][prop].push(value);
    } else if (prop == "tracks" && resProp == false) {
      object[id][prop].push(value);
    } else if (value == "" && prop != "") {
      delete object[id][prop];
    }
  }
  return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

Hello!

That’s because you didn’t take into account that the array/property may not be present.

The test that fails is trying to update the object property 5439, which doesn’t have the tracks property, causing an error:


(Picture taken using your own solution).

Fix: account for the case when the property tracks may not be present.

By the way, here’s another error:

var resProp = object[id].hasOwnProperty(prop);

If the property doesn’t exist, calling hasOwnProperty will generate an error too:

but for the case tracks does not exist , I created it.
resProp will resturn false when it doesn’t find the property track.

I had put the correction for it in another topic but they deleted it.
here it is

else  if (prop == "tracks" && resProp == false){
 object[id].prop=new array();
object[id][prop].push(value);

    }

and still the same problem.

I closed the duplicated topic, but I merged the posts of that topic in this one, nothing was deleted.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Yes. The problem is that if you’re evaluating the possibility of the property object[id] not being present, then you should move resProp inside the if (resId == true), otherwise an error will be thrown when object[id] is undefined. Do you see the problem?

That’s the problem of creating duplicate posts :sweat_smile:.

Anyway, the code is not correct either: new array() is not JavaScript syntax. The correct one would be new Array() or simply []… and even that wouldn’t work since you’re not accessing the right property: object[id][prop] is correct, object[id].prop is not, unless the object has a property called prop.

1 Like