Hey guys,
So I have managed to progress a bit but got stuck again.
2 questions.
on my 2nd if statement,
it asks " but the album doesn’t have a tracks property"
i wrote id.tracks!==0
shouldn’t it be id.tracks===0? ( that one didn’t work )
and then my 3rd if not working too :P, would love a hint ty!
**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 (prop!=="tracks" && value!==0) {
records[id][prop] = value;
}
else if (prop==="tracks" && id.tracks!==0) {
records[id][prop] = [value]
}
else if (prop==="tracks" && value!==0) {
records[id][prop].push(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/96.0.4664.45 Safari/537.36
I think you need to revisit your notes about how to check whether an object has a certain property or not (using built in JavaScript method has…, or checking whether the value is undefined), how to access a value in an object using key (with bracket or dot notation), also how to check whether an array is empty or not, and how to check whether a string is empty or not by checking their length. And adjust your codes - the ifs (…) accordingly.
Because you want to check whether the prop argument is === to the value of tracks in the object. Right now what you’re doing is checking whether prop argument is === “tracks” (as a plain string). Forever JS will evaluate it as true for the first if and false for the second and third (else) if.
The same thing with value. What you’re doing is checking whether the value argument is !== 0 (as a plain number), thus forever JS will evaluate those ifs as true, because the value argument is always a string of something that is not a number 0.
Consequently they all messed up with the result.
id.tracks is almost correct, but id itself is a property. JS does not know which object do you want to check whether it has a property of id or not. So you have to specify the object too, e.g. theobject.id.tracks to be able to access the value in that object
Search in Google or Mozilla Developer Network MDN for those methods.
Thanks I read about length it is very helpful and makes more sense.
But the lessons here don’t cover it up, that is why it is confusing whether I should complete the challenge with what the lessons have covered or actually research more stuff myself.
The .hasOwnProperty method was covered in this lesson.
There’s nothing wrong with doing outside research too.
But you do have all of the information to solve record collection.
It might help to review the freecodecamp lessons on objects to help you tackle the conditions for record collection.
the last code block you posted, if we look at the print of
console.log(updateRecords(recordCollection, 5439, 'tracks', 'Take a Chance on Me'));
we got
'5439': { albumTitle: 'ABBA Gold', tracks: 'Take a Chance on Me' } }
The tracks is not an array as required in the third bullet point. You need to tweak the if statement that did this. If we feed the test case into the function, it is clear that the test case fits the second if statement. The second if statement did this. You just need to tweak it a little so that the returned object has the value in an array
is the test case giving “tracks” as argument for prop? True.
JS checking for truthy value on this statement records[id].hasOwnProperty("tracks"). Is record 5439 has a property named “tracks”? False.
True && False evaluates to False, the test case does not fit this if statement, JS will let it slide, doing nothing.
is the test case giving “tracks” as argument for prop? True.
with records[id].hasOwnProperty("tracks") === false) JS first checks, is record 5439 has a property named “tracks”? False. Then it compares with your if statement: Is False === false? True
True && True evaluates to True, the test case fit this if statement, JS will do an operation here.
for the operation, we have:
let tracks = [];
records[id][tracks].push();
What you’re doing here is:
creating an independent variable called tracks and assign an empty array as its value. But it is an independent variable. JS doesn’t know if that tracks is suppose to be a property of 5439 object. You have to make tracks as a property of the object. How would you do that?
Since you did not connect tracks variable as a property of 5439, you are practically pushing into Undefined now. Moreover, you are pushing -Nothing- with .push().
.push(there_must_be_a_value_in_here)
Thanks for explaining me, I should remember in if statements I need a comparison, that was a bit confusing when you have more than just a regular comparison.
for your second explanation:
isn’t when the function is called with the parameters , then JS knows that records[specificID][that specific tracks] ?
it becomes recordCollection[5439][tracks] or am I wrong for thinking this way?
Yes, JS will see that as: recordCollection[5439].tracks but recordCollection[5439].tracks is currently undefined.
tracks variable you initiate with let tracks = [] will always be, by default, seen as different than recordCollection[5439].tracks unless you explicitly tell JS that they are the same thing. That’s because computer never assumes anything, so we have to be very specific.
If you’re still stuck, just let me know and I’ll show you what needs to be tweaked and why. You’re very close.
let tracks = [];
record[id][prop] = tracks;
record[id][tracks].push(value);
so I wrote like this I am still wrong.
I understood from you that record[id][prop] is undefined,
so I gave them an equal operator.
and then wrote record[id][tracks].push(value); but still wrong.
I know I asked it a few times but am I supposed to ask that many questions? a part of me feels I should figure it out on my own.
I feel like you don’t know how to debug? Copy paste the test specs under your function. Delete or comment out what’s not necessary, and console.log so you can see the result your function gave so you can compare with what’s expected, thus you know why your function fails?
like this:
function updateRecords(records, id, prop, value) {
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
console.log(updateRecords(recordCollection, 5439, "artist", "ABBA")) //, artist should be the string ABBA
console.log(updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"))// , tracks should have the string Take a Chance on Me as the last element.
console.log(updateRecords(recordCollection, 2548, "artist", ""))//, artist should not be set
console.log(updateRecords(recordCollection, 1245, "tracks", "Addicted to Love"))//, tracks should have the string Addicted to Love as the last element.
console.log(updateRecords(recordCollection, 2468, "tracks", "Free"))//, tracks should have the string 1999 as the first element.
console.log(updateRecords(recordCollection, 2548, "tracks", ""))//, tracks should not be set
console.log(updateRecords(recordCollection, 1245, "albumTitle", "Riptide"))// , albumTitle should be the string Riptide
// 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 (prop!=="tracks" && value!==0) {
records[id][prop] = value;
}
else if (prop==="tracks" && records[id].hasOwnProperty("tracks")===false) {
let tracks = []
records[id][prop] = [value];
}
else if (prop==="tracks" && value!==0) {
records[id][prop].push(value);
}
else if (value.length===0) {
delete records[id][prop];
}
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
so I finally got to the last condition.
few questions.
on the 2nd condition
why is it
records[id].hasOwnProperty("tracks")===false)
[id], and not [‘id’].
records[id][prop] = [value];
you told me I have to tell JS to make sure that let tracks = [] and recordCollection[5439].tracks is the same, but I haven’t and code still works. how come?
and my last line of condition not working why is that?
ty for your answers in advance.
The variable value holds a string. It will never be 0. The instructions want you to check for a value that is the empty string.
I think a lot of your current confusion is still related to you not understanding what each argument is. Have you tried console logging what each argument is?
good logic, but in this case it’s better to make a comparison like this one prop==="tracks", so for empty string we can do value === "". You know why? because if we do value.length === 0, and a nasty User input a blank array as an argument: , JS will evaluate it as true too. It became a bug.
for bracket and notation to access object properties, you better read this link below thoroughly. id in this case is a variable. If we do [‘id’], JS will look for a property id in the object, and in this case no object have property id, only albumTitle, artist, and tracks. While if you want to access property tracks, you have to do [“tracks”] or with dot notation: .tracks
To sum it up, if it’s a variable, we can only do [id]
If it’s not a variable, we can do: [“artist”] or .artist
What I mean each lesson on fcc has tasks at the bottom left, so you can see what you did, I don’t see the difference between that and the console.log I typed.