Hi all, I’m reviewing JavaScript and got to the Record Collection challenge quite well. I wanted to do it from scratch and I think I’m close and just a little explanation on details I’m missing will help me pass it without resorting to the solution.
So, if you’re inclined to do so, I thank you very much.
Here’s my code:
// 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;
} else if (prop === "tracks" && !object[id].hasOwnProperty(value)) {
object[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
object[id][prop].push(value);
} else if (value === "") {
delete object[id][prop];
}
return object;
}
updateRecords(collection, 5439, 'artist', 'ABBA');```
And here are the error messages:
// running tests
After updateRecords(collection, 5439, "tracks", "Take a Chance on Me"),
tracks should have Take a Chance on Me as the last element.
After updateRecords(collection, 1245, "tracks", "Addicted to Love"), tracks should have Addicted to Love as the last element.
After updateRecords(collection, 2468, "tracks", "Free"), tracks should have
1999 as the first element.
// tests completed
May it be I didn't call the array "tracks" appropriately?
Well, on that same condition you’ve got an empty array. Is that the requirement? And on the next condition you use arr.push - it will probably throw an error ‘arr is not defined’
@MrBondx Thank you. I still have issues I see.
The empty array is a requirement. And the arr in the next condition doesn’t throw an error at all. In fact the challenge seems to be working with no syntax errors until I run the tests.
Guess I’ll have to study it better. Thanks.
P.S.: I replaced the “arr” with “object[id][prop]”. Same.
@ilenia Thank you for your patience ieahleen. I added value to the empty array, I guess I’ll have to think better what property I have to check.
But talking in general, I’m better off than before, don’t you think? As I said, I still have issues but not so disastrous as before.
I’ll keep on studying and studying.
Thank you.
@Frakkoz Thank you, I had done the adding of value but didn’t add it here!
I changed it to push value instead of “tracks”.
I’m still receiving the same errors though I have no syntax errors.
So thanks, I’ll keep on trying!
@Frakkoz I was turning my head around just to figure out where I had to add a “!”! Thank you so much! I’ll check the inverted commas too, thanks to the changes you suggested I’m receiving less errors!
Well, @Frakkoz, almost there.
// running tests
After updateRecords(collection, 2468, “tracks”, “Free”), tracks should have 1999 as the first element.
After updateRecords(collection, 2548, “tracks”, “”), tracks should not be set
// tests completed