Record Collection difference between tracks and "tracks"

i was trying to complete this test and came up with this solution
// 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] = value;
}else if (prop === “tracks” && value !==“”){
if(records[id].hasOwnProperty(“tracks”)){
records[id][prop].push(value);
}else{
records[id][prop] = ;
records[id][prop].push(value);
}
}
return records;
}
updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’);|
my problem is with that records[id].hasOwnProperty(“tracks”), i only got to that after looking at the answer in the forums.
Before that i was using records[id].hasOwnProperty(tracks), almost the same but without the “” around the tracks, i know “” mean string but i don’t know why i need to use it since we don’t have “tracks” as a string in the set up.

So what is tracks in this case? It doesn’t have quotes around it so it’s not a string, so it must be the variable tracks but that variable doesn’t exist anywhere in your code. The method hasOwnProperty takes a string (or Symbol) as an argument. So if you aren’t passing a literal string into it (such as "tracks") then the variable you are passing into it better store a string value. And as I mentioned, you haven’t defined the variable tracks in your code, so it doesn’t exist when you try using it like that. You could do the following at the beginning of the function:

const tracks = "tracks";

And then it would work, but why do all that when you can just use “tracks” in the function call?

I’m not sure I understand this statement completely. You have used the string “tracks” in various places before the call to hasOwnProperty, but that would have no affect on this function call.

thank you so much for your answer.

so tracks only appears on the recordCollection object, and when it appears i isn’t declared as a string, so that’s why i am confused, since it worked that way i am assuming everything and every other object i add in recordCollection is considered a string.
And yes i have used “tracks” as a string before, but that was comparing an input from the user, as seen here prop !== “tracks” but that time i was comparing to something inside my code without any user input.
So i assumed a simple solution “if id has a property tracks then it would return true else if would return false” was my thought process, but when i tried using only hasOwnProperty(tracks) i wouldn’t work, causing me to get very confused.

so based on that everytime i try to use hasOwnProperty i should use a string as an argument?
And thank you so much for your patience, english ins’t my 1st language so i really appreciate the time you dedicate to answering my questions

Yes, because that’s what the method hasOwnProperty is expecting you to pass into it. You can either pass in a literal string such as:

obj.hasOwnProperty("tracks")

Or you can pass in a variable that stores a string, such as:

const myVar = "tracks"
obj.hasOwnProperty(myVar)

The object property names are strings. They have to be something, if they were identifiers it would be a reference error.

const user = {
  name: 'John Doe',
  age: 25,
}

console.log(Object.keys(user)[1]); // age
console.log(typeof Object.keys(user)[1]); // string

But unlike JSON you do not have to explicitly set them as strings using quotes. They can be computed and Symbols are valid as well. Numbers are coerced to strings.

const computed = "age";
const symbol = Symbol("email");

const user = {
  name: "John Doe",
  [computed + 1]: 25,
  1: "One",
  [symbol]: "john@example.com",
};

console.log(user.age1); // 25
console.log(user["1"]); // One
console.log(user.email); // undefined
console.log(user[symbol]); // john@example
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.