Record collection is this the right solution?

So I struggled a bit on this challenge and was stuck with one last check:
After updateRecords(5439, "tracks", "Take a Chance on Me") , tracks should have "Take a Chance on Me" as the last element.
I added collection[“5439”][“tracks”]=[value]; to my code and it ended up working. I was wondering if this is sort of cheating it though since I am specifically adding the “tracks” property to “5439” and setting it to [value]. Doesn’t this make the code not reusable and only works for this specific situation?
So I was wondering how else could I solve this since that is the only way I could think of to add the “tracks” property to “5439”.
Here is 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 updateRecords(id, prop, value) {
  if(prop=="tracks" && value!=""){
    collection[id][prop].push(value);
  }else if(prop=="tracks" && collection.hasOwnProperty("tracks")){
    collection[id][prop]=[];
    collection[id][prop]=[value];
  }else if(prop!="tracks" && value!=""){
     collection[id][prop]=value;
  }else{
    delete collection[id][prop];
  }
  collection["5439"]["tracks"]=[value];
  return collection;
}

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

It’s pretty much the same as is you were doing a math test to solve an equation where I tell you the result before hand, and you skip the execution and just give me the expected result.

Is it cheating? Not really…
Is it what you are supposed to do? Absolutely not.

Said that, why is your code failing?

Well one of the rules is

If prop is "tracks" but the album doesn’t have a "tracks" property, create an empty array before adding the new value to the album’s corresponding property.

But your condition check that tracks actually exist

if(prop=="tracks" && collection.hasOwnProperty("tracks"))

Once you update it, you will see two tests failing, and that’s correct, since your code is not doing what is really supposed to do :smile:

---- EDIT ----

If I may, I’ll give a little hint.
Remember that you can nest if if they have the same condition.

So for example instead of checking

is props "traks", value is not "" and prop is defined
OR
is props "traks", value is not "" and prop is  not defined

You can “group” them:

is props "traks", value is not ""
  - now is prop defined -> do a thing
  - is prop not defined -> do another thing

hope this will help you simplify your code a bit :slight_smile:

1 Like

Also, your if statement and second else if statement are swapped according to the instructions. After you create the missing array I believe that order will be important.

1 Like

I changed the code to this and have two tests failing still. I also tried changing the order like mmookow said but it made every test fail so I don’t think I did it right :(.
I don’t know how to make it so when it tests if it has the “tracks” property it comes out false. It seems like ==false doesn’t work. I also don’t think I added what Marmiz mentioned correctly because it still says push is undefined.
Edit: Nevermind, I put quotes around false and it works but I still can’t figure out the push :frowning: and adding a new “tracks” property.

Here is my code with the two tests failing:

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

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

So I ended up solving it but have a question.

If I remove the [id] here:

}else if(prop==“tracks” && !collection[id].hasOwnProperty(“tracks”))

After updateRecords(2468, "tracks", "Free") , tracks should have "1999" as the first element. and
After updateRecords(2548, "tracks", "") , tracks should not be set
end up not working.
Can someone explain why? Am I not suppose to solve it this way?

Here is the code I solved it with:
// 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]=[];
     collection[id][prop]=[value];
   }else if(prop=="tracks" && value!=""){
    collection[id]["tracks"].push(value);
   }else{
  
     delete collection[id][prop];
   }
  
  return collection;
}

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

@Liolena Object.hasOwnProperty return true if the Object has the passed prop.

So

collection.hasOwnProperty("track") // false

is false, since collection props are each “sub-object”

1245
2468
2548
5439

However each sub-object may or may not have a prop called “tracks”.
So

id = 2548
collection[id].hasOwnProperty(“tracks”) // true
// it has [ "Let It Rock", "You Give Love a Bad Name" ]

The difference hence is accessing and testing each sub-object.


Your code passes the test, so you got that going :call_me_hand:
However this whole code can be simplified.

If you go back and re-read it you’ll see that the main conditions are 3:

- prop is tracks and value is not empty -> either add or create new
- value is not empty and is not track -> add said value to prop
- value is empty -> delete said prop

Or in code:

 if (prop === "tracks" && value !== "") {
    // maybe an if / else here to know wether we have to add or create new
  } else if (value !== "") {
    // note that if we are here is because prop is NOT tracks
    // add value to prop
  } else {
     // delete
  }

Does it make sense? :slight_smile:

1 Like

Omg!! Thank you for helping I understand now :D.