Hi, below is my attempt and I couldn’t pass the test. If anyone can point out my mistakes I will be appreciated. Btw, I looked at the solution already and my code is similar to solution 2, except I avoid the OR operator since short-circuit evaluation has not been taught to me.
// 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 value is blank, delete the prop property
if (value === ' ') {
delete recordCollection[id][prop];
} else {
// If value is not blank, prop is tracks, and the tracks array exists, add value to the array
if (prop === 'tracks' && recordCollection.id.hasOwnProperty("tracks")) {
recordCollection[id][prop] = [value];
} else {
// If value is not blank, prop is tracks, but no tracks property, create an array and add value to it.
recordCollection[id][prop].push(value);
}
} else {
// If value is not blank but prop isn't tracks, set prop to value.
recordCollection[id][prop] = value;
}
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
Hi, you made me realize recordsCollection is the global variable so I changed that to just records. To address your point #2, I changed recordCollection.id.hasOwnProperty(“tracks”) into records.id.hasOwnProperty(“tracks”), is that correct? And one more question: I try to evade the OR operator but I don’t know how (maybe there isn’t a way to do that?). I’m sure the way I’m doing here is incorrect (see the second if statement).
// 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 value is blank, delete the prop property.
if (value === ' ') {
delete records[id][prop];
} else {
// If value is not blank, prop is tracks, and the tracks array exists, add value to the array.
if (prop === 'tracks' && records[id].hasOwnProperty("tracks")) {
records[id][prop] = [value];
} else {
// If value is not blank, prop is tracks, but no tracks property, create an array and add value to it.
records[id][prop].push(value);
// If value is not blank but prop isn't tracks, set prop to value.
records[id][prop] = value;
}
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
I don’t understand what you mean by “overwriting the array.” I do know the following
// If value is not blank, prop is tracks, but no tracks property, create an array and add value to it.
records[id][prop].push(value);
// If value is not blank but prop isn't tracks, set prop to value.
records[id][prop] = value;
is wrong, because if prop is equal to tracks but doesn’t have the tracks property, the second if statement will executes the else statement, which is both creating an array and set prop to value. I just code like that cause I don’t know where else to put those statements.
it depends on what your intention are, if you want to add an item to an array, the first one doesn’t do that, it creates a new array, the second one add an item to the array, yes
Well my intention is to add an item since that’s what the challenge wants, I just confused adding item with creating an array. Also this part in my code
// If value is not blank, prop is tracks, and tracks array exists, add value to the array.
if (prop === 'tracks' && records[id].hasOwnProperty("tracks")) {
records[id][prop].push(value);
} else {
// If value is not blank, prop is tracks, but no tracks property, create an array and add value to it.
records[id][prop] = [value];
// If value is not blank but prop isn't tracks, set prop to value.
records[id][prop] = value;
is where I want to mimic the following part in solution #2 where
else if (prop === 'tracks') {
records[id][prop] = records[id][prop] || []; // this is called shortcircuit evaluation, see below for explanation
records[id][prop].push(value);
} else {
records[id][prop] = value;
Is there a way to do that without using the OR operator?
I understand I can’t mix 1/2 with 3 because 3 belongs to the non-track category and 1/2 belongs to the track category. I 'm just confused on how to write the code. My problems are: even though 1/2 are in the track category, there is one difference between the two and that is whether tracks property exists or not, as well as where to put the code if prop is not tracks.
Below is what I tried:
Explanation on how I did it: the statement below the second if executes if prop is tracks and the property exists. The one below the first else executes if prop is tracks but the property does not exist. The one below the second else executes if prop is not track.
Once I done that, I get the error message at the last line of code that looks like this