Basic JavaScript - Record Collection

I don’t even know what to type…there are so many instructions that I don’t even know where to start. I know I have to make an if else statement probably, but don’t know even what to start writing ): I understood previous lessons.

Tell us what’s happening:
Describe your issue in detail here.

  **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) {



return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');



/* Hint 1
Use an if...else if chain to check the needed steps.

Hint 2
To access the value of a key in this object, you will use records[id][prop].

Hint 3
You can’t push to an array that doesn’t exist. Use hasOwnProperty to check first.
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

You are correct, it’s a bunch of if/else statements. Start at the beginning of the list and work your way down. Each bullet is a different if statement.

2 Likes

I tried finding a hint, and I was so stuck that I looked for the solution. When reading the solution, I am super confused as to why it is the answer – no explanation…just shows the answer, which doesn’t help :grinning:

Here is the first part of the answer. What I don’t understand is why records isn’t in but id and prop are inside them. Could someone break this down for me? (sorry — learning disability and it’s hard for me to understand abstract concepts)


// Only change code below this line
function updateRecords(records, id, prop, value) {
  if (prop !== 'tracks' && value !== '') {
    records[id][prop] = value
  }

The solution is not the point. Ignore it. The point is practicing breaking down a problem so you can come up with a process and turn it into code.

records is an object, id is a number, and prop is a string. Do you remember the lessons on how to access properties of objects?

1 Like

Oh yeah! In fact, I have been practicing by making my own just to grasp the concept! But, as you can see, I am getting “undefined” lol

  {
    'bedrooms': 4,
    'bathrooms': 2,
    'year_built': 1993
  }
]
myHouse.bedrooms

And why can’t you do

records.id.prop?

your code about bedrooms appears to be incorrect(or maybe its a part of code?)
it will work like this:

let usersObj = {
    'bedrooms': 4,
    'bathrooms': 2,
    'year_built': 1993
  }
  
console.log(usersObj.bedrooms)//4

id and prop are variables, when you are using variables to access stuff, you need to use brackets, such explanation is in the curriculum

See the challenge here:

1 Like

But I was able to access it by using . unless it was a string that had a space

Nope. You cannot mix dot notation with property names stored in variables. Dot notation means you have the exact, literal, character-for-character property name hardcoded in.

const sampleObj = {
   'bedrooms': 4,
   'bathrooms': 2,
   'year_built': 1993
};
 
// Good
console.log(sampleObj.bedrooms); // 4

// Good
console.log(sampleObj['bedrooms']); // 4

// Good
const prop = 'bedrooms';
console.log(sampleObj[prop]); // 4

// Bad
console.log(sampleObj.prop); // undefined

But for this challenge that I am having trouble with, where is the property name stored in variable?

Function arguments are variables

Duh! Lol thanks. So records is a variable then too, so wouldnt it be [records]?

No. You use brackets when trying to access a property of an object when the property name is stored in a variable. You also use brackets when you access an array element at a specific index. You do not have to wrap every single variable in brackets.

So then is this right so far?

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' && album.hasProperty('tracks'))


  return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

There is no variable album

Then what would I add there I wonder? " * If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it."

How could you determine the specific album you should be modifying?

Each album has a unique id number as its key and several other properties.

It would be ID right?