Tell us what’s happening:
I kinda understand what I need to do. But whatever I try, it doesn’t seem to work. Can you tell me what I’m doing wrong?
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 records.id[prop];
} else if (prop !== "tracks" && value !== "") {
records[id][prop] = records[id][value];
} else if (prop === "tracks" && value !== "" && id.hasOwnProperty("tracks") === false) {
records[id][tracks] = [];
tracks.push(value);
} else if (prop === "tracks" && value !== "") {
records[id][tracks].push(value)
}
return records;
}
console.log(updateRecords(recordCollection, 5439, 'artist', 'ABBA'));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Basic JavaScript - Record Collection
sanity
March 25, 2024, 8:44am
2
Try focusing at a single case at a time. Otherwise it’s easy to get distracted by all the things that might need keeping track of.
Taking as example the one from code:
console.log(updateRecords(recordCollection, 5439, 'artist', 'ABBA'));
The relevant id printed in console from what function returns:
(...)
'5439': { albumTitle: 'ABBA Gold', artist: undefined }
(...)
Consider what does it mean that artist
got added undefined
? Which part of code added it and why? What’s the related user story for this case?
I think that instead of “what added "undefined"
?” it’s more of “which part of code isn’t working?”. Cause it it should’ve added “ABBA” ?
sanity
March 25, 2024, 9:04am
4
That’s one way to look at it, both are related. Saying isn’t working is really it isn’t working as intended, because it should do (…) . However at all times it is working the way it’s in the code.
} else if (prop == 'artist' && value !== "") {
artist.push(value);
}
I’m trying to make it put artist’s name from value into artist
sanity
March 25, 2024, 9:11am
6
That’s not in the original code? What’s the artist
variable, where is it defined?
There’s no "artist"
in the original code. I need to add that property to 5439
sanity
March 25, 2024, 9:20am
8
I’m talking about artist
variable from artist.push(value)
. Unless artist
variable is declared somewhere that will always give error.
} else if (prop === "artist" && value !== "" && id.hasOwnProperty("artist") === false) {
records.artist = [];
artist.push(value);
}
So, I’ve this. I just kinda created an empty array for the artist’s name to go to. The reason why I didn’t have this before is bc I only thought about it after I talked to you.
sanity
March 27, 2024, 7:36am
10
That creates new property on the record
, but the name of it is literally "artist"
.
console.log(record.artist) // []
However there’s still no artist
variable declared, so artist.push(value)
will end up with error.
system
Closed
September 25, 2024, 7:36pm
11
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.