I manage to solve this problem within 30 minutes but I am not sure if this is actually how it is done. I know I already solved the problem but I am just curios if my approach is somewhat ok and I just want to contribute in the Forum as well maybe.(have something posted in the forum)
Your code so far
// Setup
var collection = {
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(object, id, prop, value) {
if(prop != "tracks" && value != ""){
object[id][prop] = value;
}
if(prop == "tracks" && object[id].hasOwnProperty(prop) == false){
object[id][prop] = [value];
}
if(prop == "tracks" && value){
object[id][prop].push(value);
}
if(value == ""){
delete object[id][prop];
}
return object;
}
console.log(updateRecords(collection, 2468, '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/87.0.4280.141 Safari/537.36.
I’ve edited your post.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.
Oh sorry about that. I’ll do it next time I’ll post a question. But can you please tell me why I need to add spoiler tags in my post? Is it to avoid giving answer to others?
a few things to considered:
(note, your code works, and that is a really good first milestone, from there you can only improve)
if you use many if statements each one is evalauted indipendently, if you use an if/else if/else chain then once the first true one is found, the others are not evaluated, there is not much resources saved in a small thing like this, but it is a good thing to always consider, even just for the habit
using string equality is always preferred, ===, same for inequality, !==. If you don’t have a really good reason to use the loosely equal, then don’t. This avoids unexpected behaviors when comparing some things.
proper indentation make your code easier to read, it’s not a language for which indentation is part of the logic, but it makes it more readable:
function updateRecords(object, id, prop, value) {
if (prop != "tracks" && value != "") {
object[id][prop] = value;
}
if (prop == "tracks" && object[id].hasOwnProperty(prop) == false) {
object[id][prop] = [value];
}
if (prop == "tracks" && value) {
object[id][prop].push(value);
}
if (value == "") {
delete object[id][prop];
}
return object;
}
you have used value as condition, which may be mroe readable if you explicitly write value !== "", but then you have put the comparison in object[id].hasOwnProperty(prop) == false , here as hasOwnProperty returns a boolean you don’t need to use a comparison, you can just put the ! (NOT) operator in front of it to flip the boolean value: !object[id].hasOwnProperty(prop)