Questions about lesson 93 record collection

This is an exercise/lesson in the Basic Javascript section of Javascript Algorithms and Data Structures. The first solution has something confusing to me in the first else if statement. In the line above, value is not in square brackets, but it is in brackets in the else if. Is it because if “tracks” does not exist (which is an array), then it creates the array called tracks? Here is the function:

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 !== "") {
  } else if (value === "") {
    delete records[id][prop];
  return records;

Your explanation sounds correct to me. Is there something specific you don’t understand about the code? Because it seems like you are reading it correctly.

Yes, it’s sort of doing the same as if you had assigned an empty array and then pushed some value.

records[id][prop] = [];
const someValue = 'Hello';
const makeMeAnArray = [someValue];

console.log(makeMeAnArray); // [ 'Hello' ]

I don’t think I would have known to put [] around the one value. I tried to do that exercise but didn’t get far before I was stumped. So I keep going back to it until I “get” it. I also noticed comments on the pages that have the solution after clicking “Get a hint”. I’ve made notes on the exercises that were difficult for me. I’ll have to go back and read the comments as well for more insight.

Got it - thanks! I’ll eventually understand the little things that are getting me stuck

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