// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if(prop !=="tracks"&&value!==""){
collection[id][prop]=value;
}else if(prop ==="tracks"&& !collection[id].hasOwnProperty("tracks")){
collection[id][prop]=[value]; //Is this line creating a new array with new value?? :create an empty array before adding the new value to the album's corresponding property.???
}else if (prop ==="tracks"&& value !==""){
collection[id][prop].push(value);
}else if (value===""){
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, âartistâ, âABBAâ);
So this code runs, but can anybody suggest how to tidy up my codes?
i know itâs a mess and i might duplicate same if statement unnecessarily.
I edited this post since I was told not to duplicate same post again.
Please shed some light here.
1 Like
if(collection[id][prop]) { //what does this line stand for?? return true/false if id.prop exists?
That statement above will check if there are any values associated with collection[id][prop].
For example, if you pass in â2548â as id and âartistâ as prop, your if statement will evaluate to be true because
it has a value which in this case is âBon Joviâ
Similarly,
collection[id][prop] = value; //??? I donât understand this line
if everything stays the same and you give value
something like âBruno Marsâ, then you are replacing âBon Joviâ with âBruno Marsâ.
Hope this helps!
1 Like
Tell us whatâs happening:
Your code so far
// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if(prop==="tracks" && collection[id][prop]["tracks"]){
collection[id][prop] = [];
}else if (prop ==="tracks"&& value!==""){
collection[id][prop].push(value);
}else{
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection
// running test
After updateRecords(5439, âartistâ, âABBAâ), artist should be âABBAâ
Cannot read property âtracksâ of undefined
After updateRecords(1245, âalbumâ, âRiptideâ), album should be âRiptideâ
// tests completed
this is what i see when i run this.
what am i missing?
thanks in advance.
Okay, so this is what Iâve got so far.
// Only change code below this line
function updateRecords(id, prop, value) {
if(prop !==âtracksâ&&value!==âemptyâ){
collection[id][prop]=value;
}else if(prop ===âtracksâ&& !collection[id].hasOwnProperty(âtracksâ)){
collection[id][prop]=[value]; //Is this line creating a new array with new value?? :create an empty array before adding the new value to the albumâs corresponding property.???
}else if (prop ===âtracksâ&& value !==""){
collection[id][prop].push(value);
}else if (value===""){
delete collection[id][prop];
}
return collection;
}
// running test
After updateRecords(2548, âartistâ, ââ), artist should not be set
// tests completed
can you give me another hint?
and I also want to know how to tidy my code better because i know it is a mess. thanks.
Tell us whatâs happening:
Your code so far
// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if(prop !=="tracks"&&value!=="empty"){
collection[id][prop]=value;
}else if(prop ==="tracks"&& !collection[id].hasOwnProperty("tracks")){
collection[id][prop]=[value]; //Is this line creating a new array with new value?? :create an empty array before adding the new value to the album's corresponding property.???
}else if (prop ==="tracks"&& value !==""){
collection[id][prop].push(value);
}else if (value===""){
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection
// running test
After updateRecords(2548, âartistâ, ââ), artist should not be set
// tests completed
//**and I also want to know how to tidy my code better because i know it is a mess. thanks.
The only problem I see is your first if statement.
if(prop !=="tracks"&&value!=="empty")
you shouldnât compare value with âemptyâ.
Hint, look at your other else if statement.
else if (prop ==="tracks"&& value !=="")
ê”łë!