Request for code review on Record Collection challenge

Hey, first time posting. My code below works for the Record Collection challenge, but I would love some feedback on how this can be improved. It feels spaghetti to me and I would appreciate suggestions on how to polish it.

Thank you.

    // 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) {
      var theProp = prop;

      if (Array.isArray(collection[id][theProp]) && value !== '') {
    collection[id][theProp].push(value);
      } else if (collection[id].hasOwnProperty(theProp) === false && theProp === 'tracks' && value !== '') {
    collection[id][theProp] = [];
    collection[id][theProp].push(value); 
      } else if (collection[id].hasOwnProperty(theProp) === false) {
    collection[id][theProp] = value;
      } else if (value === '') {
    delete collection[id][theProp];
      } 
      
      return collection;
    }

    // Alter values below to test your code
    updateRecords(5439, "artist", "ABBA");
    updateRecords(2468, "tracks", "Free");
    updateRecords(2548, "tracks", "");
    updateRecords(5439, "tracks", "Take a Chance on Me");
    updateRecords(2548, "artist", "");

To make your code easier to read. Put three (3) back tick marks (usually left of the 1 key on keyboard) at the start of your code and three more back ticks after your code.

As far as feedback. Your code works which is most important, so congratulations to you! There are a couple modifications you can make to reduce the amount of code while keeping your algorithm logic the same.

On line # 36 of your code you have:

  } else if (collection[id].hasOwnProperty(theProp) === false && theProp === 'tracks' && value !== '') {

You can simply this else if with the following:

  } else if (!collection[id].hasOwnProperty(theProp) && theProp === 'tracks'' && value !== '') {

I did two things above. 1st I added the ! before collection and removed the ==== false part. This is another way of saying the same thing. You can do the same for line #39.

Also on line #41 of your code you have:

  } else if (value === '') {

I believe you can simply make it an else statement without the if condition, because based on your code logic, it appears it is implied that value has something in it if you get to this point. So just change line #41 to:

  } else {

The only other suggestion is more a matter of opinion of what looks “cleaner”, but anytime I have an if or an else if or and else statement where there is only one statement to be executed afterwards, I forgo the { }. JavaScript will assume if it does not see { } after those if statements to execute the next line only and then treat all following lines as separate code. The following is an example for future reference. Let’s say I had the following if …else if…else statements:

if (color == "red") {
   return "my favorite color";
}
else if (color == "blue") {
   return "my friend's favorite color";
}
else {
   return 'no body likes this color';
}

could be changed to:

if (color == "red") 
   return "my favorite color";
else if (color == "blue") 
   return "my friend's favorite color";
else 
   return "no body likes this color";

Awesome, thanks for letting me know about the markup tip for this forum and for the advice.