Record collection understanding the exercise

Tell us what’s happening:
Describe your issue in detail here.
so to solve the problem, we have to basically use conditional.
according the second , we have to write:
if(prop != tracks…); but here prop is a property.
as i have learned in the previous lesson to access the property i need to use dot and bracket notation. but in this case i have accessed only by calling it’s name(prop). Why is that?

  **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) {
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/90.0.4430.93 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

prop is a string, which also happen to be one of the property names of an object
but first of all it’s a variable holding a string, as such you can check the value of the variable with !=
If you want to get the value of the property with that name, then you need to use dot/bracket notation

@ilenia

  1. it is holding a string? i didn’t get this one
    2.“If you want to get the value of the property with that name, then you need to use dot/bracket notation”…could you explain this simply or with an example it will be very helpful. thank you
let object = {
   country: "Italy",
   capital: "Rome"
}

let’s consider this object, this object has two properties, each has a name and a value

if we have a variable, called prop, like so

let prop = "country"

this prop variable holds a string, which also happen to be a property name
we can check the value of this variable

prop != "capital" // true
prop != "country" // false
prop == "capital" // false
prop == "country" // true

but that is just the value of the variable
we know it’s also a property name, and if we want to get the value of the property we need to use bracket notation (you can’t use dot notation with variables)

object[prop] // Italy
1 Like

but i didn’t declare prop as a variable here? neither did i assign it to a name in object .so why can i still compare it as a string holder or a string itself?
prop was not mentioned in the object also …so is it that automatically objects property are denoted as prop?
how can records be a object literal, there is dissimilarities in it’s syntax.
for ex; it should be- records={ }
@ieahleen

The function arguments are defined outside of your function and passed in. You don’t declare or define the values of the arguments inside of your function. That defeats the purpose of reusable functions.

records is always an object, like the sample recordCollection

id is always a record id

prop is always a string of a property name

value is always a value to set or an empty string to indicate deletion

so if i write the arguments in this order
then first one will be object, second one will be id , third one will be prop , fourth will be value ? or such that,
it depends on the keyword itself( for ex: id for indicating id)?

If id is a record id
prop is a string of property name mentioned in the records object
value is the value of the object records
Why is it comparing with the datas inside the record collection object?

function parameters can be named in any way you want, but the description tells you that the function will be called with 4 arguments, of which

  • the first one is an object, the object that has to be updated,
  • the second one a number, a property of the object passed as first argument,
  • the third a string, which is a property of the inner object
  • the fourth is a string too, and it is the value to use to update the property with the name equal to the third parameter, or an empty string, and then the property is to remove

they could be named in any way, in this case the prewritten parameters are records, id, prop, value

but you could also totally write

function updateRecords(potato, orange, maple, ruby) {
}

and potato is the passed in object, orange is the id, maple is the property name and ruby the value to use to update the maple property

like any variable, function parameter names are totally arbitrary

here the recordCollection object is passed in as first argument and it will be referenced with records or potato in the function, 5439 is passed in as second and it will be referenced as id (or orange, 'artist' is passed in as third argument, and it will be referenced as prop (or maple), and 'ABBA' is the fourth argument and it will be referenced as value or ruby
each time the function is called with different values, those values will be referenced inside the function using the parameters

1 Like

so it mainly depends on the argument i give,
updateRecords(records, …)
so if i had called updateRecords(5439, …);
then records would have been a id instead of an object?

yes, but you need to make a function that works with that order of arguments as that is what the tests give you

i am sorry…didn’t get it.
could you explain more

the challenge will call your function with the arguments in a certain order, so you need to follow that order in making your function.

this is correct, but you need to consider the function calls done by the tests
those will use the arguments in the predetermined order

so you are telling me for the test ,as it is already determined i have to follow the order. but,
if i make my own function i can use it in any order

for your projects you can do what you want yes

if you do challenges like this, or contribute to already existing projects some things will already be predetermined

1 Like
 else if(prop==="tracks"&&value!="") {
    records[id][prop][tracks].push(value)

updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me") , tracks should have the string Take a Chance on Me as the last element.
can you tell why my above code is not fulfiling the following task?

add the function call in the editor to test it and see if you get any error

TypeError: Cannot read property ‘hasOwnProperty’ of undefined

else if (prop==="tracks"&&records[id][prop].hasOwnProperty("tracks")==false) {

    value=[];

any problem in this line?

a few issues, yes
hasOwnProperty is an object method, you need to use it on an object, not an undefined value
you are also overwriting the function parameter value


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).