Push function issue

This code refused to run, I keep getting error message that .push() is not a function. I can’t figure out what the problem is… need help please!!

  **Your code so far**

// Setup
const recordCollection = {
2548: {
  albumTitle: 'Slippery When Wet',
  artist: 'Bon Jovi',
  tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
  albumTitle: '1999',
  artist: 'Prince',
  tracks: ['1999', 'Little Red Corvette']
},
1245: {
  artist: 'Robert Palmer',
  tracks: []
},
5439: {
  albumTitle: 'ABBA Gold'
}
};

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

updateRecords(recordCollection, 5439, 'tracks', 'Take a Chance on Me');


  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36

Challenge: Record Collection

Link to the challenge:

It’s easier to debug if we console.log the test cases.
console.log(updateRecords(recordCollection, 5439, 'tracks', 'Take a Chance on Me'));
result:

  '5439': { albumTitle: 'ABBA Gold', tracks: 'Take a Chance on Me' } }

It happens because of the second if statement:

else if(prop === "tracks" && records[id].hasOwnProperty("tracks") === false){
    records[id][prop]=value;
}

A little tweak and you’ll pass the test.

IMO, Side note, if a function is expected to be done and stop when it found the first case that fits the criteria for an operation, we’d want to Return right away. If we don’t return right away, JS will continue to evaluate the case with the next if/else if statements. In some situation, this will mess up with the result (e.g. JS doing more than one operations because the case fits more than one if statements)

Thanks Stephen for helping me out. But I still need more clarification. Do I need to modify the second “else if” statement with a return type?

My last paragraph is just a suggestion. You can ignore my last paragraph and still pass the specs.
Look at the result of this console.log

console.log(updateRecords(recordCollection, 5439, 'tracks', 'Take a Chance on Me'));

and see what the function is returning for that case, and compare with the bullet points of what is actually expected for the return, and tweak that second else if.

Thanks Steve, I tweaked the second else if and it ran successfully. Thanks a lot for your help.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.