DEV Community

Randy Rivera
Randy Rivera

Posted on

Challenge Record Collection

Record Collection:

You are given a JSON object representing a part of your musical album collection. Each album has a unique id number as its key and several other properties. Not all albums have complete information.

Here we have an updateRecords function that takes an object like 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.

  • The function must always return the entire object.
  • If prop isn't tracks and value isn't an empty string, update or set that album's prop to value.
  • If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it.
  • If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array.
  • If value is an empty string, delete the given prop property from the album.
  • Solve:
var collection = {
  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 below this line.
function updateRecords(object, id, prop, value) {
  return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');
Enter fullscreen mode Exit fullscreen mode
  • Answer:
var collection = {
  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'
  }
};

var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line

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

console.log(JSON.stringify(updateRecords(collection, 5439, "tracks", "Take a Chance on Me"))); will display
{"1245":{"artist":"Robert Palmer","tracks":[]},"2468":{"albumTitle":"1999","artist":"Prince","tracks":["1999","Little Red Corvette"]},"2548":{"albumTitle":"Slippery When Wet","artist":"Bon Jovi","tracks":["Let It Rock","You Give Love a Bad Name"]},"5439":{"albumTitle":"ABBA Gold","tracks":["Take a Chance on Me"]}}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
maxnyby profile image
Info Comment hidden by post author - thread only accessible via permalink
maxnyby

If prop is “tracks” and the object doesn’t have a property “tracks” already, and value is an empty string, then the “tracks” property is initialized with an array containing a single empty string.
This corresponds with your documentation, but is it indeed the intended behavior?
Afterwards it is not possible to add further empty strings to the tracks property, so this might be a glitch?

Some comments have been hidden by the post's author - find out more