Hello everyone,
I am asking for help to complete this challenge. For some reason, I just keep failing all the tests each time …so if you can tell me what I’m doing wrong then it would be much appreciated.
**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(value === ""){
delete recordCollection[id][prop];
}
else if(prop === 'tracks' || recordCollection[id].hasOwnProperty("tracks") !== true){
recordCollection[id][prop] = [];
recordCollection[id][prop].push(value);
}
else{
recordCollection[id][prop] = value;
}
return records;
}
// updateRecords(recordCollection, 5439, 'artist', 'ABBA');
console.log(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/95.0.4638.69 Safari/537.36
My main comment is that you would find the task easier if you arranged your if else statements in the same order that the conditions are given:
1 If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
2 If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
3 If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.
4 If value is an empty string, delete the given prop property from the album.
What can I set my default statement to, at the end of else if chain …? Or is it not needed to have an else statement at the end of the else if chain…? I really don’t completely know that is why I’m asking this.
Solve the issues then return records. I’ve written some psuedo code below.
You return afterwards beacuse:
Your function must always return the entire record collection object.
if( problem1 ) { solution to problem 1 }
else if ( problem 2){ solution to problem 2}
else if( problem 3){ solution to problem 3}
else if( problem 4){ solution to problem 4 }
return records;
One more thing. You can’t solve this unless you get your condions right. Your first one looks way off. it should be (and I haven’t tested this) more like:
// If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
if(prop !== "tracks" && value !== "")
Before I do though You should note that you should be referencing records in your function as opposed to recordCollection That is what makes the function reuseable in different contexts
Oh Okay, I’ll do it. So some of them are passing now… I guess that’s what I was missing but the ones where the ‘prop’ should be deleted when the value is not set, are not passing.
So I did shift the lines of code like you said and referenced the records correctly…I don’t know how I was this stupid…and it worked. So Thanks a lot for getting me through this challenge and hopefully I wasn’t too annoying.
Hope you have a great rest of your day .