Why variable won't work

I am just wondering why the variable I created (properties), will not work? The if statement works when I do records[id][prop] = value but not when I use the variable which is the same code? I’m just trying to understand what is happening. Thanks!

// 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) {
    let properties = records[id][prop];  
  if (prop !== 'tracks' && value !== ''){
      properties = value;
  }return records;


updateRecords(recordCollection, 5439, 'artist', 'ABBA');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15

Challenge: Record Collection

Link to the challenge:

If we are assigning / reassigning a value to an (original) “object” we want to do it on the original “object”, not the “name” that we deemed representing the “object”.

A variable is just a name we give to an “object”, not the real “object” itself.

Declaring let properties = … here is like taking a screenshot of a paper titled “records[id][prop]”, print it on a new paper with a title “properties”. Thus, when we make a scribble on the paper titled “properties” guess what, the paper titled “records[id][prop]” remain intact…

If that makes sense to you?

  1. Your updateRecords function isn’t closed with a curly brace }
  2. When you are setting the properties variable to records[id][prop] you are getting the value at that location and setting that value to the variable properties, not making an alias for records[id][prop]. Then you change the value held by the properties variable to the passed in value held by the ‘value’ variable. This is basically the equivalent of just using;
    let properties = value;
    Where the variable properties will have no correlation to the records object.

You can place a console.log(properties); just after both uses of the variable and see what the outputs are to further help your understanding.

This should result in:
undefined
ABBA

undefined is due to the 5439 record not having a ‘artist’ property.

1 Like

It makes sense now. Thank you.

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