Struggling to understand, I m trying my best to make the problem small as possible but my head hurts, not sure where to start e.g. the first test says ‘if value is empty then delete the prop’ now I got the following
but where should I put the word value as i cannot think outside of records[id][prop] == ''
How can I make the problem small as possible and build on top of it?
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(records[id][prop] == ''){
delete records[prop]
}
console.log(records)
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36
Yes, that is how you would check if value is an empty string.
I’d suggest you get in the habit of using === unless you need the type coercion of ==. An empty string is also a falsy value so that is also an option if (!value)
if(value == ''){
delete records[prop]
}
You are missing something here. How do you know what object to delete the property from? By its…?
key. if the answer is key, I am doing that when I do delete records[prop] which says delete record with the given prop.
Do not answer the solution, I really wanna know what I m doing wrong. where is my missing logic
function updateRecords(records, id, prop, value) {
if(value === '')
delete records[id][prop];
if(prop !== 'tracks' && value !== ''){
records[id][prop] = value
}
if(prop == 'tracks' && value !==''){
if(!records[id].hasOwnProperty('tracks')){
records[id]['tracks'] = [];
records[id]['tracks'].push(value);
}
}
return records;
}
What shocks me most, why I was making it so complex like if(records[id][props] === value) thinking it will check if(records[id][props] === ''). My question now would be, why people like me make it so complated and is it normal, what is best way to approach a problem especially complex one.
As said, plenty of people struggle with this one and it isn’t that easy to get right. But it would have been better if you posted your updated code and continued to get help with it.
Code often starts out a bit verbose, after you get it working you can refactor the code. The opposite can also be true, you may find a bunch of edge cases later on that you didn’t account for, and the code balloons.
In the end, it comes down to experience.
There is definitely an argument to be made that the function has too many responsibilities. You are more likely to have three functions, one for creation, one for updating, and one for deletion. The way it uses an empty string argument to delete data is a fairly questionable API decision I would say.
It looks like you didn’t answer for 43 hours, replied, waited 32 hours, and then looked at the answer? Unfortunately those gaps pushed the conversation into some personal business of mine (and sorta broke up the flow!).
Did you try anything else between you posting your code 5 days ago in the original post and looking at the answer? There was a pretty big gap with no new code shown.
I’d definitely not look at answers - this becomes increasingly important from this point on.
This is a bit strangly phrased but seems correct to me.