First, I will assume you did not mean to post two of the same line (below)
function updateRecords(id, prop, value) {
If my assumption is correct, then you are actually wondering why the following code does not work. I took the liberty of properly indenting your code to make it more readable.
function updateRecords(id, prop, value) {
if(value){
if(collection[id][prop]){
collection[id][prop].push(value);
}else{
collection[id][prop] = value;
}
}
return collection;
}
Your first if statement ask if value evaluates to true. As long as value is not a falsy value (0, “”, NaN, undefined, null), then this if statement will always evaluate to true which will cause the 2nd (nested) if statement to ask if collection[i][prop] evaluates to true. Again as long as the value of collection[id][prop] is not a falsy value, then it will execute the following line, which attempts to push value into collection[id][prop]. The push method will only work on arrays. So, if collection[id][prop] is not an array, then your code will error out.
collection[id][prop].push(value);
Let’s examine one of the test cases which your solution faiils.
After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.
In this test case value = “Take a Chance on Me”, so your first if statement evaluates to true, so then we evaluates the 2nd if statement. Since collection[id][prop] is undefined, because prop is “tracks” and there is no “tracks” property for id of 5439, your else block of code (shown below) executes and assigns the “tracks” property the string “Take a Chance on Me”.
Because the tracks property should contain an array of one element ( the string “Take a Change on Me”), you fail the test case.