# Struggling with Record Collection Challenge

**URL:** <https://forum.freecodecamp.org/t/struggling-with-record-collection-challenge/442167>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [January 22, 2021, 8:44pm UTC](https://forum.freecodecamp.org/t/struggling-with-record-collection-challenge/442167 "2021-01-22T20:44:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clasped003](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/clasped003/32/184248_2.png) [@Clasped003](https://forum.freecodecamp.org/u/Clasped003)\
**Post date:** [January 22, 2021, 8:44pm UTC](https://forum.freecodecamp.org/t/struggling-with-record-collection-challenge/442167/1 "2021-01-22T20:44:54Z")

</div>

**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**

```js

// 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:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [January 22, 2021, 9:14pm UTC](https://forum.freecodecamp.org/t/struggling-with-record-collection-challenge/442167/2 "2021-01-22T21:14:48Z")

</div>

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.

```auto
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.

> [@Clasped003](#):
>
> ```auto
> collection[5439].artist = "ABBA";
> collection[5439].tracks = ["dancing queen"];
> 
> ```

---

<div class="post-metadata">

**Author:** ![GeorgeCrisan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/georgecrisan/32/90008_2.png) [@GeorgeCrisan](https://forum.freecodecamp.org/u/GeorgeCrisan)\
**Post date:** [January 22, 2021, 11:14pm UTC](https://forum.freecodecamp.org/t/struggling-with-record-collection-challenge/442167/3 "2021-01-22T23:14:38Z")

</div>

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

```auto
// 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:

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![Clasped003](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/clasped003/32/184248_2.png) [@Clasped003](https://forum.freecodecamp.org/u/Clasped003)\
**Post date:** [January 23, 2021, 12:00am UTC](https://forum.freecodecamp.org/t/struggling-with-record-collection-challenge/442167/4 "2021-01-23T00:00:47Z")

</div>

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

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Clasped003](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/clasped003/32/184248_2.png) [@Clasped003](https://forum.freecodecamp.org/u/Clasped003)\
**Post date:** [January 23, 2021, 12:03am UTC](https://forum.freecodecamp.org/t/struggling-with-record-collection-challenge/442167/5 "2021-01-23T00:03:12Z")

</div>

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:

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Clasped003](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/clasped003/32/184248_2.png) [@Clasped003](https://forum.freecodecamp.org/u/Clasped003)\
**Post date:** [January 23, 2021, 2:30am UTC](https://forum.freecodecamp.org/t/struggling-with-record-collection-challenge/442167/6 "2021-01-23T02:30:38Z")

</div>

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.

* * *

```auto
// 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');

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 23, 2021, 6:24am UTC](https://forum.freecodecamp.org/t/struggling-with-record-collection-challenge/442167/7 "2021-01-23T06:24:03Z")

</div>

> [@Clasped003](#):
>
> object[id][value]

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 23, 2021, 6:26am UTC](https://forum.freecodecamp.org/t/struggling-with-record-collection-challenge/442167/8 "2021-01-23T06:26:19Z")

</div>

@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.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](http://www.freecodecamp.com/forum/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [July 24, 2021, 6:26pm UTC](https://forum.freecodecamp.org/t/struggling-with-record-collection-challenge/442167/9 "2021-07-24T18:26:23Z")

</div>

Closed on July 24, 2021, 6:26pm UTC

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