Record Collection (Statement positioning)

Alright so I passed this challenge yesterday and have reset it 6-7 times to do it over again and pass again. I really want to fully grasp this theory, so I don’t mind spending a couple days studying it. So my question is the positioning of the if/else, else if statements. Originally I placed the “delete collection” segment at the bottom of the code and followed instructions 1-3 at the top and it passed. Then I placed the delete at the top and put one of the push statements on the bottom as you can see here. It passed. Then I tried putting the statement with the empty array and push at the bottom and it everything failed for one condition. Then I tried setting the value at the end as well and it failed two conditions. Can someone explain in more detail whats going on, I know its the way the statements are structured.
Thanks!!

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"
}
};

// Only change code below this line
function updateRecords(id, prop, value) {
if (value == "") {
delete collection[id][prop];
} else if (prop != "tracks" && value != "") {
collection[id][prop] = value;
} else if (prop == "tracks" && collection[id].hasOwnProperty(prop)==false) {
collection[id][prop] = [];
collection[id][prop].push(value);
} else {
if (prop == "tracks" && value != "") {
  collection[id][prop].push(value);
}
}

return collection;
}

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/83.0.4103.97 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

If I were in your shoes I would use http://pythontutor.com/javascript.html#mode=edit or the debugger of your browser’s dev tools + breakpoints to step through the various scenarios you mentioned and see exactly what is going on there … maybe draw a sketch of your observations along the way …

1 Like

I didn’t even know that existed lol, thanks!

1 Like

Alright so I ran 2 visualizations and one of them reads the first if statement and goes straight to the bottom to "return collection ", the other reads the first if statement then the else/if statement, then goes straight to the bottom again to “return collection”. So all the code in-between the arrows never got ignored? Because both of them worked in the challenge and I was assuming all the lines of codes get read

Mhh no … only the parts it logically reaches …

1 Like