Tell us what’s happening:
Hi. Thought I could do this without asking for help but it’s not working . I seem to be failing in more ways than I can describe.
Am I accessing my props correctly?
Am I mixing up my id and records parameters?
And after finding the previous challenges on arrays quite easy, I can’t seem to be able to create an array and that’s just demoralizing!
Your code so far
// Setup
var 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(id[prop]!=='tracks'&&prop[value]!==''){
records[id]=value;
} else if(id[prop]=='tracks'&&records[id].hasOwnProperty('tracks')==false){
newArray=[].push(value);
} else if(id[prop]=='tracks'&&value!==''){
tracks.push(value);
} else if(id[value]===''){
delete records[prop];
}
return recordCollection;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
in the initial “if” condition , you are only checking the parameters of the function , I don’t think you have to access records to check prop and value.
if (prop !== “tracks” && value !== “” )
then to assign the value ; remember records is a collection of objects , you access an individual record with record[id] and you assign the property ‘prop’ to 'value with:
Well, that explains it. The function arguments are not defined by those names outside of the function. The function arguments are only defined by those names inside of the function.
The purpose of that advice is to get you to look at what those four things actually are because the way you are using them is not consistent with the type of data stored in those variables.
Oh, now I get it. The records, for example, is some kind of object, id is a number etc. Now I’ll rewrite my codes, hopefully knowing what the parameters are will help me to use them consistently.
Still can’t pass this after several attempts. I’d like to know if my usage of the given parameters is still inconsistent with the data type of each parameter. Also, is my logic even correct?
// Only change code below this line
function updateRecords(records, id, prop, value) {
console.log(id); // Is 'id' an object?
console.log(id.hasOwnProprety('tracks')); // Can 'id' have a tracks array?
if (prop !== 'tracks' && value !== '') {
// This is good - you are *updating* the value of the property
records[id][prop] = value;
} else if (prop == 'tracks' && id.hasOwnProperty('tracks') === false) { // You should use === over ==
// You should not declare a new variable here
// You need to *update* the appropriate property
// "If prop is tracks but the album doesn't have a tracks property,
// create an empty array and add value to it.""
// This empty array should be for the tracks property!
var newArray = [].push(value);
} else if (prop == 'tracks' && value !=='') {
// And here you need to 'push' onto the tracks property array
newArray.push(value);
} else if (value = '') { // = is not ===
delete records[id][prop];
}
return recordCollection; // You should not reference the global variable
}