lundi 21 juin 2021

Record Collection: "If" Statements in JavaScript

Good morning, I am working on this problem on freecodecamp trying to learn JavaScript and I am unsure of where to go with it. I personally feel the wording is off, however I understand that I need to use "If" statements.

The problem says:

You start with an updateRecords function that takes an object literal, records, containing the musical album collection, an id, a prop (like artist or tracks), and a value. Complete the function using the rules below to modify the object passed to the function. Your function must always return the entire record collection object.

  1. If prop isn't tracks and value isn't an empty string, update or set that album's prop to value.

  2. If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it.

  3. If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array.

  4. If value is an empty string, delete the given prop property from the album.

Note: A copy of the

recordCollection object is used for the tests.

The code I have right now

// 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' && (!(recordCollection[id]).tracks)) {
    recordCollection[id].tracks = [];
  } else if (prop.tracks) {
    prop.album
  }

  return records;
}

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

Really in particular it's the bottom part that I've actually written. But I am at a loss and I am not sure how to interpret the requirements of these if statements.

I am appreciative of any kind of help that I can get on this. I don't want it to stifle me, but I am finding problem solving to be a bit of challenge for me more than I am finding Javascript syntax to be an issue.

Thank you!

Aucun commentaire:

Enregistrer un commentaire