Need help with Record Collection

Need help with Record Collection
0.0 0

#1

I get the right output but couldn’t clear this test. However I change my code fourth and fifth conditions fail. Is there any mistake in my code?

Failed conditions:

  1. After updateRecords(1245, “tracks”, “Addicted to Love”), tracks should have “Addicted to Love” as the last element.
  2. After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element

// 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"
    },
    add:function add(id,prop,value){
      [].push.call(this[id][prop],value);
    }
};
// 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(collection[id].hasOwnProperty(prop)){
    if(value==""){
       delete collection[id][prop]; 
      //alert("good day sir");
    }else{
      if(prop=="tracks"){
        collection.add(id,prop,value);
      }else{
        collection[id][prop]=value;
      }
    }
  }else{
      if(prop=="tracks"){
        collection[id][prop]=[];
        collection[id][prop].push(value);
      }else{
        collection[id][prop]=value;    
      }
    
  }
  return collection;
}

// Alter values below to test your code
updateRecords(1245, "tracks", "Addicted to Love");


// 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"
    },
    add:function add(id,prop,value){
      [].push.call(this[id][prop],value);
    }
};
// 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(collection[id].hasOwnProperty(prop)){
    if(value==""){
       delete collection[id][prop]; 
      //alert("good day sir");
    }else{
      if(prop=="tracks"){
        collection.add(id,prop,value);
      }else{
        collection[id][prop]=value;
      }
    }
  }else{
      if(prop=="tracks"){
        collection[id][prop]=[];
        collection[id][prop].push(value);
      }else{
        collection[id][prop]=value;    
      }
    
  }
  return collection;
}

// Alter values below to test your code
updateRecords(1245, "tracks", "Addicted to Love");



#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

There is no such object method named add.


#4

I forgot to add the collection. Now help me with this program.


#5

Adding the collection object does not change the fact that you are trying to use a method named add which does not exist for objects in JavaScript.

What are you trying to achieve with the above code anyway?


#6

With If block I want to push the element into the tracks array.
Else block just changes the value of the existing property.

The output is right. But still the conditions are not satisfied. Kindly test it once yourself.


#7

I’ve solved it by moving the ‘add’ function 's code to where it is required directly.

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