Stuck here atRecord Collection

Tell us what’s happening:
Hi, I am stuck here and don’t know what went wrong. The tutorial shows that only one condition has not been met. I have checked my selection method and pretty sure there is nothing wrong with that. The example “5439, tracks, ‘take a chance on me’” should be subjected to the method of collection.id.prop=value. But it is not working, I don’t know what did I omit, the hint suggests that it is not a function. Is there a problem with the use of hasOwnProperty?? I’m really not sure, can someone help me?

Your code so far


// 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 updateRecords(id, prop, value) {
  if(value===""){
    delete collection[id][prop];
  }
  else if(prop==='tracks'){
    if(collection[id].hasOwnProperty(prop)===true){
      collection[id][prop].push(value);
    }else {
      collection[id][prop]=value; 
    }
  }else if(prop!=='tracks'){
    collection[id][prop]=value; 
  }
  
  return collection;
}

// Alter values below to test your code
updateRecords(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/72.0.3626.119 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection

Actually no, because there is already a track present, it should be subjected to the collection[id][prop].push(value); treatment, which you are doing correctly.

One thing I’ve noticed you are doing incorrectly is the part immediately after, namely the else clause here:

    if(collection[id].hasOwnProperty(prop)===true){
      collection[id][prop].push(value);
    }else {
      collection[id][prop]=value; 
    }

The tracks property always must be an array, so you actually want to say collection[id][prop] = [value] there

Finally, the last else if can just be a plain else, though it doesn’t really affect the logic one way or another.

Remember that collection[id]["tracks"] should be an array

1 Like

that’s done it!. Thank you, so if I wanted to add a value to an array without using .push, the value then must be placed in a bracket?

If you want to create an array you need to put the content of the array inside brackets, yes

Correct. Array literals are made with square brackets, so [value] is an array with just one element

I see, funny how I don’t remember learning about this at all, is there a relevant challenge in the tutorial??

Oops, I thought that if I put brackets around value, then ‘value’ would be the name, instead of the content, of the array

From this onward you learn about arrays:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/store-multiple-values-in-one-variable-using-javascript-arrays

The characteristic about arrays is that the items inside an array are surrounded by the square brackets

You’ve already been doing previous challenges where you create arrays. Again, [value] is not special syntax, it’s just a literal . [1,2,3] is an array with three elements, [1,2] has two elements, and [1] just has one.