Record Collection - help required

Hi, I’ve been trying to do Record Collection for a couple hours. My solution turned out very similar to all the other, actually working solutions, but it does’t work. I would appreciate if someone have helped me understand what I’m doing wrong,

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



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

What is this doing? It isn’t doing what you think!

I got it, I guess I didn’t understand how to call methods properly

else if (prop=='tracks' && records[id].hasOwnProperty(prop)==false) {
  records[id][prop]=[value];
  return records;
}

Hi. I have a question. I have answered this step employing if/else, however I wanted to also try using switch because I wanted to learn both before I proceed further. However, I can’t seem to get it right. I’ve rewritten my if/else code to almost the same exact code apart from using switch cases, but somehow it doesn’t work. I figured I might’ve botched some syntax. Can you please take a look and tell me where I might’ve made a mistake:

function updateRecords(records, id, prop, value) {
  switch (records, id, prop, value) {
    case prop != "tracks" && value != "":
      records[id][prop] = value;
    case prop == "tracks" && records[id].hasOwnProperty(prop) == false:
      records[id][prop] = [];
      records[id][prop].push(value);
    case prop == "tracks" && value != "":
      record[id][prop].push(value);
    case value == "":
      delete records[id][prop];
  }
  return records;
}

It’s practically the same code as my if/else .

I added the missing break;s, still don’t work

A switch is not a general purpose replacement for an if-else statement.

In a switch you match one variable against multiple possible values. You could do that here, but it won’t be one switch covering all of your current if else options.

Yeah I have already posted about this and got my answer, about switch requiring one expression. Still, thank you for taking time to reply

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