Basic JavaScript - Record Collection

Tell us what’s happening:

This is my first experience with javascript.

In this follow challenge we have a function with parameters:
function updateRecords(records, id, prop, value) {…} - so far all right.
But during looking for a solution I found out, that we have to assing those parameters to each other? But how? And why should it work?
I can imagine, the function parameters are independent variables like getAgeOfAPerson(name, age, id, sex){…}, so we can change and manage them to achieve what we need, and the same with function(records, id, prop, value)…
But how can we assign those parameters to each other e.g. “name[age][id] = sex” or like in our case: “records[id][prop] = value”??? Why? What is the sense? How does it work? :exploding_head:
Im sorry, but I totally dont get this challenge so far and I would be thankful for any help, additional info, links or explanation :confounded: :see_no_evil:

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 !== ""){ records[id][prop] = value; }
  else if (prop === "tracks" && value !== "" && records[id].hasOwnProperty("tracks") === false) { records[id][prop] = [value];}
  else if (prop === "tracks" && value !== "") { records[id][prop].push(value);}
  else if (value === "") { delete records[id][prop];}
  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/109.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

What does this line of code do?

And what is the first argument given?

Does this answer your question?

not really,
how do the system knows, that records is recordCollection? And what is id, and that artist and tracks are props but not value?
How do the system know what is value in this case? I didn`t even mention smth. about that in my const recordCollection above :thinking:

It knows because you told it.
Right here:

The first argument being passed in to updateRecords is the entire recordCollection object.

Do you see that now?

Edit: you may want to review the topic that introduced function parameters

thank you, but in this challenge we use just param1 and param2 to concantinate “Hello World”. It becomes confusing, when we do assign parameters to each other like records[id][prop] = value;. What when we would do smth. like that: param1 = param2? “Hello” = “World”? It makes no sense…
And why is id array here? I mean records[id], if I understand it correctly id is the number like ‘5439’ so why do we need those brackets?

function addTwoNumbers(firstNumber, secondNumber) {
  return firstNumber + secondNumber
}

addTwoNumbers(3, 7) // returns 10

You create a function with whatever parameters you need. These parameters can take any name you choose (though it’s sensible to give them names which makes sense in the context of what they’re being used for). You can then do whatever you wish with these named parameters within your function.

When the function is called, whatever values are supplied to those parameters will be handled inside the function as defined. If bad values are supplied by the user, the function will simply spit out a nonsense result or an error (unless of course you handle this within your function).

As for dot and bracket notation for objects, bracket notation must be used if the object property is a variable: https://plainenglish.io/blog/javascript-dot-notation-vs-bracket-notation-which-to-use-when-e24117e44d71

It makes perfect sense to say records[id][prop] = value because records is an object not a literal string like “Hello”.

You can modify an object and delete and add to it. That is perfectly normal thing to do to an object.

well, thank you for your dilligence. :pray:
I think, at this time I just go further and will think about it a little bit later.

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