Javascript Record Collection. Help me make my code cleaner &&simipler

// 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(prop !=="tracks"&&value!==""){
    collection[id][prop]=value;
  }else if(prop ==="tracks"&& !collection[id].hasOwnProperty("tracks")){
    collection[id][prop]=[value];   //Is this line creating a new array with new value?? :create an empty array before adding the new value to the album's corresponding property.???
  }else if (prop ==="tracks"&& value !==""){
    collection[id][prop].push(value);
  }else if (value===""){
    delete collection[id][prop];
  }



   return collection;
  

 }

// Alter values below to test your code
updateRecords(5439, “artist”, “ABBA”);


So this code runs, but can anybody suggest how to tidy up my codes?
i know it’s a mess and i might duplicate same if statement unnecessarily.
I edited this post since I was told not to duplicate same post again.
Please shed some light here.

1 Like

if(collection[id][prop]) { //what does this line stand for?? return true/false if id.prop exists?

That statement above will check if there are any values associated with collection[id][prop].

For example, if you pass in “2548” as id and “artist” as prop, your if statement will evaluate to be true because
it has a value which in this case is “Bon Jovi”

Similarly,

collection[id][prop] = value; //??? I don’t understand this line

if everything stays the same and you give value something like “Bruno Mars”, then you are replacing “Bon Jovi” with “Bruno Mars”.

Hope this helps!

1 Like

Tell us what’s happening:

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(prop==="tracks" && collection[id][prop]["tracks"]){
    collection[id][prop] = [];
  }else if (prop ==="tracks"&& value!==""){
    collection[id][prop].push(value);
  }else{
    delete collection[id][prop];
  }



   return collection;
  

    }

  
 


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

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


// running test
After updateRecords(5439, “artist”, “ABBA”), artist should be “ABBA”
Cannot read property ‘tracks’ of undefined
After updateRecords(1245, “album”, “Riptide”), album should be “Riptide”
// tests completed

this is what i see when i run this.
what am i missing?
thanks in advance.

Okay, so this is what I’ve got so far.

// Only change code below this line
function updateRecords(id, prop, value) {
if(prop !==“tracks”&&value!==“empty”){
collection[id][prop]=value;
}else if(prop ===“tracks”&& !collection[id].hasOwnProperty(“tracks”)){
collection[id][prop]=[value]; //Is this line creating a new array with new value?? :create an empty array before adding the new value to the album’s corresponding property.???
}else if (prop ===“tracks”&& value !==""){
collection[id][prop].push(value);
}else if (value===""){
delete collection[id][prop];
}

return collection;

}

// running test
After updateRecords(2548, “artist”, “”), artist should not be set
// tests completed

can you give me another hint?
and I also want to know how to tidy my code better because i know it is a mess. thanks.

Tell us what’s happening:

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(prop !=="tracks"&&value!=="empty"){
    collection[id][prop]=value;
  }else if(prop ==="tracks"&& !collection[id].hasOwnProperty("tracks")){
    collection[id][prop]=[value]; //Is this line creating a new array with new value?? :create an empty array before adding the new value to the album's corresponding property.???
  }else if (prop ==="tracks"&& value !==""){
    collection[id][prop].push(value);
  }else if (value===""){
    delete collection[id][prop];
  }



   return collection;
  

    }

  
 


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

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


// running test
After updateRecords(2548, “artist”, “”), artist should not be set
// tests completed

//**and I also want to know how to tidy my code better because i know it is a mess. thanks.

The only problem I see is your first if statement.

if(prop !=="tracks"&&value!=="empty")

you shouldn’t compare value with “empty”.

Hint, look at your other else if statement.

else if (prop ==="tracks"&& value !=="")

ê”łëŸ­!