0

I'm a newbie so please don't be harsh on me.

Here's the object and the link to it:

// 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'
  }
};

Here's the solution:

function updateRecords(records, id, prop, value) {
  // Access target album in record collection
  const album = records[id];

  // If value is an empty string,
  //  delete the given prop property from the album
  if (value === "") {
    delete album[prop];
  }
  // If prop isn't tracks,
  //  update or set that album's prop to value
  else if (prop !== "tracks") {
    album[prop] = value;
  }
  // If prop is tracks,
  //  add value to the end of the album's existing tracks array
  else {
    album["tracks"] = album["tracks"] || [];
    album["tracks"].push(value);
  }

  // Your function must always return the entire record collection object
  return records;
}

I passed the challenge and then tried to do more and check - if the given value exists then do nothing. I tried to do it with hasOwnProperty() and indexOf(), but I always get a TypeError:

TypeError: Cannot read properties of undefined (reading 'indexOf')

While googling the correct way of doing it, i only found even bigger functions just to check if the given value exists. Could you help me, please? How would you do it?

1
  • if (album[prop] === value) // do nothing ? Commented Aug 16, 2022 at 20:54

2 Answers 2

1

The test for whether the value is already included in tracks should be after you initialize it if necessary.

function updateRecords(records, id, prop, value) {
  // Access target album in record collection
  const album = records[id];

  // If value is an empty string,
  //  delete the given prop property from the album
  if (value === "") {
    delete album[prop];
  }
  // If prop isn't tracks,
  //  update or set that album's prop to value
  else if (prop !== "tracks") {
    album[prop] = value;
  }
  // If prop is tracks,
  //  add value to the end of the album's existing tracks array
  else {
    album.tracks = album.tracks || [];
    if (!album.tracks.includes(value)) {
      album.tracks.push(value);
    }
  }

  // Your function must always return the entire record collection object
  return records;
}

Sign up to request clarification or add additional context in comments.

Comments

1

First check if the property exists, if not add it. Then use includes to see if that track exists.

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'
  }
};

function updateRecords(records, id, prop, value) {
  // Access target album in record collection
  let album = records[id];

  // If value is an empty string,
  //  delete the given prop property from the album
  
  if(prop in album == false && prop == "tracks"){
     album[prop] = [];
  }
  
  if (value === "") {
    delete album[prop];
  }
  // If prop isn't tracks,
  //  update or set that album's prop to value
  else if (prop !== "tracks") {
    album[prop] = value;
  }
  // If prop is tracks,
  //  add value to the end of the album's existing tracks array
  else if(album[prop].includes(value) == false){
    album["tracks"].push(value);
  }

  // Your function must always return the entire record collection object
  return records;
}

console.log(updateRecords(recordCollection, 5439, "tracks", "Let It Rock"));

1 Comment

.includes() is the modern way to test, instead of .indexOf != -1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.