Basic JavaScript - Record Collection

This is really challenging. After dozens of attempts only 2 of the tests passed, i think i wrote the conditions as instructed but my statements are very suspect and i am unsure of how to end the chain with an else statement and dont know what i am doing incorrectly. Please help!

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

else {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/113.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

This part is where things get a bit off. You put one bullet point in two else if clauses but they should be together.

You can’t put this return in an else - the first instruction is to always return the records object.

else if (records[id].hasOwnProperty("tracks") == false ){
  records[id]["tracks"] = [];
  if (prop == "tracks" && value != ""){
    records[id][prop].push(value);
  } 

thanks for pointing that out, i rewrote to this but now having issues with the 4th test only which is confusing as there are tests with similar parameters and ticked valid, must be something i missed but cant see it at all.
I removed all the return keywords from my statements, did that help why almost all tests got valid? if yes, please why?

return records;

Also removed the else as pointed out and left as instructed, i now understand that else statements are only necessary when all other conditions aren`t true.

Please post all of your code.

It looks like you deleted the logic for when the tracks array is already there? You need to address all of this last bullet

If prop is tracks and value isn’t an empty string, you need to update the album’s tracks array. First, if the album does not have a tracks property, assign it an empty array. Then add the value as the last item in the album’s tracks array.

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

  return records;
}

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

Here is the full code.

You need to look at this.

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

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

I don`t know what i am doing wrong, nothing i try works. The annoying thing is test id 1245 and id 5439 have the same parameters and 5439 is valid but 1245 remains invalid and at a loss why, it is the only one of my tests that is not working. Am i navigating incorrectly to the object to access it? Please help.

Back up. Read the instructions one piece at a time

If prop is tracks and value isn’t an empty string, you need to update the album’s tracks array.

This is the main check for the else if /\

First, if the album does not have a tracks property, assign it an empty array.

This is a subcheck

Then add the value as the last item in the album’s tracks array.

This is another subtask

1 Like
else if (prop == "tracks" && value != "" ){
   if (records[id].hasOwnProperty("tracks") == false){
     records[id]["tracks"] = [];
   }
   else (records[id].hasOwnProperty("tracks") == true);
records[id][prop].push(value);
}

I feel so ridiculous now, it was literally staring at me …lol. After 3 days of mental agony, thank you so much for helping.

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