Basic JavaScript - Record Collection

Help!
I have a problem with the else if criteria. Everything is working but I can’t seem find the problem with the else if side

  **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) {
if (value !== "") {
  records[id][prop] = value
} > else if (recordCollection.hasOwnProperty("tracks"), prop === "tracks" && value !== "") {
>   records[id][prop].push(value);
} else {
delete records[id][prop]
}
 

 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/15.1 Safari/605.1.15

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

This condition is not correct

You should not directly reference the global object

Also, you cannot put a comma in the condition like that

Hi there, Sorry for the late reply.
About the condition not being correct, will ‘prop !== “tracks” && value !== “”’ work ?

My guess is no. Why don’t you try it.

it actually did.
the only thing left to complete this stage is " * After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"), tracks should have the string Take a Chance on Me as the last and only element."

And what did you mean by global object please

He meant that you are trying to access recordCollection which is defined in the global scope instead of records which is what the function was given.

Ohh thank you very much. And what can I do to replace the comma ?

what you want the condition to say? what did you use the comma for?

what i want the condtion to mean is, if records has a property called tracks and prop is strictly tracks and value is not empty… add whatever value might be to records[id][prop].
Also if you can help me with [After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me") , tracks should have the string Take a Chance on Me as the last and only element.]- what does last and only element mean ?

You can use the same structure you have used here to describe it in your own words: … AND … AND …

means that it should be an array with only one element inside

Hmm, Just tried it and it doesn’t seem to work well with it. Could you please suggest another approach to tackle it ?

what code have you tried? please show your updated code

function updateRecords(records, id, prop, value) {
  
  if (prop != "tracks" && value !== "") {
    records[id][prop] = value
} else if (records.hasOwnProperty("tracks") && prop === "tracks" && value != "") {
    records[id][prop].push(value);
} else {
  delete records[id][prop]
}
   
  
   return records;
}

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

This won’t work. The records object won’t have the property “tracks”.

// 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 (records.hasOwnProperty("tracks") && prop === "tracks" && value != "") {
    records[id][prop].push(value);
} else {
  delete records[id][prop]
}  
   return records;
}

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

records still doesn’t have a “tracks” property

1 Like
// 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 (records[id][prop].hasOwnProperty("tracks") && prop === "tracks" && value != "") {
    records[id][prop].push(value);
} else {
  delete records[id][prop]
}  
   return records;
}

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

The only thing I could thing of was records[id][prop].hasOwnProperty(“tracks”).

What is records?

What is records[id]?

What is records[id][prop]?