Record Collection Woes

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

I don’t see why this code does not work. It plays out so logically in my head. Unfortunately, my brain is not the compiler.

ERROR CODE: SyntaxError: unknown: Identifier ‘prop’ has already been declared. (23:36

  **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) {
let prop = tracks || artist;
if (prop != tracks && value != "" ){
  prop = value;
}
if (prop = tracks && recordCollection.hasOwnProperty("tracks") == ""){
tracks.push(value);
}
if (prop = tracks && value != ""){
  track.push(value);
}
if (value == ""){
  delete recordCollection.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/102.0.5005.63 Safari/537.36

Challenge: Record Collection

Link to the challenge:

Below is one of the challenge solutions from FCC.
When i just use “tracks” in my code it tells me that “tracks” is not defined. How did the person in the below solution get away with it?

// Setup
var 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’);

How do you assign a value to a variable in JavaScript? ie what is the syntax for doing that?

How do you check if two values are equal in JavaScript? ie what is the syntax for that?

Take the answer to these questions, then with that in mind look at your code. What is the error you have made throughout the code?

Dan,

Thanks for stopping by.
I know you are trying to give me a “stronger” hint that leads me to a waterfall of realization. but, you are going to have to hit me over the head with it.

Once again here is my code -

function updateRecords(records, id, prop, value) {
var prop = “tracks” || “artist” ;
if (prop != tracks && value != “” ){
prop = value;
}
if (prop = tracks && recordCollection.hasOwnProperty(“tracks”) == “”){
tracks.push(value);
}
if (prop = tracks && value != “”){
track.push(value);
}
if (value == “”){
delete recordCollection.prop;
}

return records;
}

I have wracked my brain for ways to fix my code, i have changed the “let” command to a “var” command.

Which gets rid of the below error -

SyntaxError: unknown: Identifier ‘prop’ has already been declared. (23:36)

But then throws a new error -

ReferenceError: tracks is not defined

I’m still lost as to how tracks is still undefined, to my knowledge i have defined it using this line of code.

var prop = “tracks” || “artist”

spoiler with fixes

function updateRecords(records, id, prop, value) {
  // var prop = “tracks” || “artist” ;
  const album = records[id] // save ref to our album for readablity
  // if (prop != tracks && value != “” ){
  if (prop !== "tracks" && value !== "") {
    // prop = value;
    album[prop] = value;
  }
  // if (prop = tracks && recordCollection.hasOwnProperty(“tracks”) == “”){
  else if (prop === "tracks" && !album.hasOwnProperty(prop)) {
    // tracks.push(value);
    album[prop] = [value];
  }
  // if (prop = tracks && value != “”){
  else if (prop === "tracks" && value !== "") {
    // track.push(value);
    album[prop].push(value);
  }
  // if (value == “”){
  // delete recordCollection.prop;
  else if (value === "") {
    delete album[prop];
  }

  return records;
}

Thank you,
This makes so much sense.
Coding can be so annoying. I’m beginning to realize that with this challenges i might need to define entirely new variables to get it to work.
Thanks a million, This really helped !