Struggling with Record Collection Challenge

Tell us what’s happening:
Hi, I am at the Record collection challenge and am trying to avoid spoilers, so I am posting to see if what I have done so far (from the code I have entered) will work to complete the first three requirements. There are 5 requirements total, I will list my issues starting with the first. I am very new to JS and am still trying to get comfortable with these challenges that tbh are pretty difficult. I have reviewed the lessons a few times regarding objects, but whats different with this challenge is that a lot of it seems to take place from within a function, not just outside of one, which is what is throwing me off.

1st requirement( * Your function must always return the entire object.): This doesnt seem to be an issue since the return is already set for the object to be returned. I don’t have to do anything to this, right?

2nd requirement (If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value.): For this, I added an if statement as follows:

if (prop != “tracks” && value != “”){
prop = value;
}

is this correct? or am I going in the wrong direction? Should I create two variables for prop and value in this function and then pass those along instead? I feel like this the way I already have it is enough.

3rd requirement(If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.): I am stuck on adding the Track value to the id 5439. I know I can add this the way I have it written, but it sounds like the challenge wants me to add this stuff through the inside of the function. It also seems like it wants me to write a more generalized operation that could apply to any given id, not just id 5439. I am just not aware how to do this from within a function instead of outside. Is that correct? If so, how do I manipulate the properties of the object from within the function? The lessons that seem to cover this all seem to do with doing this stuff from outside of a function so it seems that even if I try to assign a variable to the property for track from within the function, I would need to still be specific to the track id 5439.

Any help is really appreciated!
Your code so far


// Setup
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 code below this line
collection[5439].artist = "ABBA";
collection[5439].tracks = ["dancing queen"];

console.log(collection[5439]);

function updateRecords(object, id, prop, value) {
// 1st if statement
if (prop != "tracks" && value != ""){
  prop = value;
}
return object;
}

updateRecords(collection, 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/87.0.4280.141 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

  1. The album’s prop and the parameter prop is not the same thing. The prop parameter is just a string value. You need to use the prop parameter to get to the object property.
const users = {
  1: {
    name: 'Jack',
    age: 32
  },
  2: {
    name: 'Jill',
    age: 28
  }
}

function getUserAge(users, id, prop) {
  return users[id][prop];
}

console.log(getUserAge(users, 1, 'age')); // 32
  1. You need to use the id parameter to get to the correct object, then check for the track property on the object. If the object does not have the property you need to create it, assign it an array, and add the value to the array.

Side note: Be careful about changing the object before the function call. I would suggest you do not touch the object outside of the function.

Try to reset the lesson and write your solution inside updateRecords function block.

// Only change code below this line
function updateRecords(object, id, prop, value) {
  /** The logic goes here, you will need some if else statements and stuff. good luck. **/
  return object;
}

Pseudo possible solution:

function updateRecords(object, id, prop, value) {
  if (value is empty) delete the prop at the provided id;
  else if (the prop is 'tracks') {
    add the value to the existing prop array or create an array if does not exist
  } else {
    if the prop is not tracks then must be other one
    in this case just set the value to the corresponding prop
  }

  return object;
}

hi, is this better for the 2nd requirement?:

function updateRecords(object, id, prop, value) {

  if (object[id][prop] !== "tracks" && object[id][value] !== ""){

    object[id][prop] = object[id][value];

  }

  console.log(object[id][value]);

  return object;

just wondering, would this work to satisfy the 2nd requirement? I see what you posted but I was wondering if I could solve and code for the requirements in order:

function updateRecords(object, id, prop, value) {

  if (object[id][prop] !== "tracks" && object[id][value] !== ""){

    object[id][prop] = object[id][value];

  }

  console.log(object[id][value]);

  return object;

Here is an updated version with me still trying to piece everything together, I ended up just adding the logic and trying to see if it will work but it wont start up. I feel like i’m most of the way there.


// Setup

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 code below this line

//1st if statement

function updateRecords(object, id, prop, value) {

  if (prop !== "tracks" && value !== ""){

    object[id][prop] = object[id][value];

  }

  //2nd if statement

  if (prop === "tracks" && object.hasOwnProperty("tracks") == false){

    object[id][prop] = emptyArray[value];

  }

  //3rd if statement

  if (prop === "tracks" && value !== ""){

    object[id].push(value);

  }

  // 4th if statement

  if (value === ""){

    delete object[id][prop];}

      

  console.log(object[id][value]);

  return object;

}

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

value is not one of the properties so this is always undefined

@Clasped003

I’ve edited your posts for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).