Record Collection Fundamental Problems

Dear Members!

The record collection task poses such a problem to me I don’t even understand the solution (see below).

I have (at least) two fundamental problems with understanding the solution:

  1. Why are “else if” statements necessary, why don’t “if” statements work?

  2. Why can certain properties in the function be adressed at one point indepenently and indivdiually (such as; prop =" tracks") and at another point can only be accessed by adhering to the hierarchy within the object (records[id][prop]?

Thanks for any input!


// 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" && 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/100.0.4896.75 Safari/537.36 Edg/100.0.1185.36

Challenge: Record Collection

Link to the challenge:

What does your solution look like. Before diving into somebody else’s code, its important to have some working solution of your own to compare with.

If you don’t have a solution, we can help you work on what you have so far.

It’s often much easier to work forward from your code than backwards from someone else’s. If you want to share your own (in progress) solution, we can try to help you get un-stuck.

Broadly speaking though,

else is used when we only want to execute a block of code when the previous condition was false.

if (age < 13) {
    console.log("child");
}
else if (age < 20) { // only if they are not under 13 and are under 20
    console.log("teenager");
}
else { // only if both ifs were false
    console.log("adult");
}

prop is an argument variable of the function, so we can check it against a string (“tracks”). In order to access a value on the object, we need to reference it by its property.

1 Like

@ArielLeslie thanks for the example on “else if”. I think I understand your example, but I don’t understand the necessity of “else if” statements in the task “Record Collection”. I was hoping the execution of the code would check every conditional within the if-statements from the first if to the last and just skip to the next one if it evaluated as false… Where am I wrong within this assumption?

It depends on how you would do it. If you want to share your sequence of if statements and logic we can help figure out how to make it work.

I was using combinations of accessing data in a try-and-error-fashion (which I hope to replace with knowledge XD). My draft of the first if-statement was something like this:

  if (records[prop] !== records[tracks] && records[value] !== '') {
    records[prop] = records[value];
  }

In the explanation of the task it is mentioned that “records” is an object, so why can I access properties of that object individually?

The properties of the records object all numbers (id numbers) such as “5439”, and “1245”.

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'
}
};

Half of this challenge is understanding the structure of this object. It is an object that contains many objects inside of it.

It seems to me the function somehow dissolves the ID number from the object, but how does this work without accessing the ID number via the object?

I am not sure what you mean, but this definitely doesn’t happen.

You have an object inside of an object. In order to get the properties of the inner objects, you use something like parentObject["innerObjectName"]["propertyName"].

The function has an argument for the id number of the record you want.

The function has an argument for the id number of the record you want.

I’m getting more confused… are the four arguments transfered to the function in a non-hierachial fashion? Like “records” is not a parent Object to “id” and “prop” and “value” are not properties of the object “id” ?
Are these four arguments something a user would type into a form?

Yes. Each argument of a function is independent. records is an object and id, prop, and value are strings.

Ok thanks, finally the conditional part of the if-statement makes sense to me since a comparison of strings is taking place

  if (prop !== 'tracks' && value !== "") {}

… but why is the left side of the equation not sinply “prop”? Suddenly (to me) records becomes a parent Object of id, the latter having the propertie “prop”… when and where did that happen? Or does this lie within the powers of the coder at this particular section of the code?

    records[id][prop] = value;

You should be asking yourself ‘what is everything’ when you get a new challenge. When I’m not sure, I start logging things out:

function updateRecords(records, id, prop, value) {
  console.log("records is", records);
  console.log("id is", id);
  console.log("prop is", prop);
  console.log("value is", value);
  console.log("records[id] is", records[id]);
  console.log("records[id][prop] is", records[id][prop]);
}

What do you see if you try this?

What about if you try other inputs?

Some return values, some “undefined”; I tried different combinations of the four arguments logging data to the console, but didn’t understand why certain statements returned value and others “undefined” or even “reference error”.
Why can you access “id” both individually and through its class but the same thing doesn’t apply to “2468”? You have to adress the latter via the class “recordCollection”…

Some data types in Javascript are:

  • Number

  • Boolean

  • String

  • Array

  • Object

Can you tell me

  1. What is id?

  2. What is prop?

You seem to be missing what these variables actually mean.

I can’t tell for sure; id seems to be a number but records[id] is an object with properties.
prop is set to different properties of instances of the class “recordCollection”, but I don’t see the logic behind all this

id is a number and records[id] is an object, that is correct.

So you need the id number to know how to look up the specific record you want. It’s similar to how you need the catalogue number to find a book at the library.

prop is a string, the name of the specific property that you want to update.

So, big picture, the challenge is saying roughly this:

You have a big box of records. Grab the one with a particular id number and update a particular property listed on the cover with this value.

1 Like

I still don’t get why four arguments are passed to the function when the first argument is an object that contains the other arguments (depending on the actual album information). If “records” and “id” weren’t structurally linked, there would be no output of “records[id]” within the function.

I just tried the first two if-Statements of the task:

  • If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
  • If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
  if (prop !== "tracks" && value !== ""){
   records[id][prop] = value;
  }

  if (prop === "tracks" && records[id].hasOwnProperty("tracks")){
    let arr = [];
    arr.push(value);
  }

In the first if statement, why is the first prop within the conditional statement accessed directly and in the executive part, it is accessed via “records[id][prop]” ?