Tell us what’s happening:
Hello guys, i know that this one is hard but i wanna solve it without looking for the solution. Anywas i’m trying to target tracks but with the id target doesn’t work. How i can target it properly? Obviously there’s more problem but this like the 1st step.
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'
}
};
const tracks = recordCollection.id
console.log(tracks)
// Only change code below this line
function updateRecords(records, id, prop, value) {
return records;
}
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/114.0.0.0 Safari/537.36 OPR/100.0.0.0
Okey but in this case is a record inside of year, i don’t know how to target every single year without writting the number indeed. My logic was that id would allow me to enter to elements inside but i was wrong. If i write every single number to get access all tracks, it would be hardcore instead of smart or efficient. Anyways thank you for provide an answer!
That doesn’t change the general idea - if you have in variable, ie. as a string, the property which should be accessed, then bracket notation needs to be used.
To give another example:
const tracks = { 1: 'Let It Rock', 2: 'You Give Love a Bad Name' };
const id = 1;
console.log(tracks.id); // undefined
console.log(tracks[1]); // Let It Rock
console.log(tracks[id]); // Let It Rock
// console.log(tracks.1); // SyntaxError
hey everyone i archive a plateau with this challenge with the part " * If prop is tracks and value isn’t an empty string, you need to update the album’s tracks array. First, if the album does not have a tracks property, assign it an empty array. Then add the value as the last item in the album’s tracks array."
my 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 !== ""){
value = records[id][prop]
}
return records
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');