Record Collection lesson

hey guys
edit code link : https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection#

this is the code I have written, and it is not working.

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

// Only change code below this line
function updateRecords(records, id, prop, value) {
  if (prop!==recordCollection.tracks && value!==0) {
    prop===value;
  }
  else if (prop===recordCollection.tracks && recordCollection.tracks===0) {
    const tracks =[value];
  }
  else if (prop===recordCollection.tracks && value !==0) {
    recordCollection.tracks.push(value); 
  }
  else if (value===0) {
recordCollection.artist = 0;
recordCollection.tracks = 0;
  }
  else {
  return records;
  }
}

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

Now to me, obviously it makes perfect sense and it needs to work, but it isnā€™t working because I have made mistakes.
I am stuck. I would love pointers to direct me in the right way.
I donā€™t want to look at the answer because I rather figure it out by myself, with some help.
Ty!

edit: A question I have, I added else to end the else if statement and I wrapped {} around the return statement after I do that I donā€™t have any completed assignments is it because the whole code is wrong, and is that why the one completed assignment that had been as the default is gone now?

Hi @TheOctagon3323 ,
It makes it easier for us to help if you include the link to the problem. otherwise we have to go searching for it.
As a matter of interest any problem you are struggling with you can request help for by pressing the Get Help button and selecting ā€œask for helpā€.
It creates a forum post complete with your code and any links or information that we might need to debug it.

Sorry, I have added now.

Hey, So Iā€™ve looked at your code and spotted most of the issues. Ill try and give you some hints.

Hint 1) One of the best ways to solve a problem like this is to make sure that you solve it systematically. You have broken down the problem into four sections but you should focus on making sure that each one is working as intended before moving on.

Hint 2) A good thing to do is to create variables and console.log() their values to make sure they are what you intended.

Hint 3) donā€™t give yourself unnecessary work
prop!==recordCollection.tracks
has less room for error if insted you try:
prop !== "tracks"

Hint 4) what is the type of value?

Hint 5) Your solution to the line below is quite far from the mark. try using the hasOwnProperty() method. you can research it here
" If prop is tracks but the album doesnā€™t have a tracks property, create an empty array and add value to it."

for hint number 3 : I tried using tracks but it says it is not defined how come I should put double quotes around it? is it because itā€™s a property and it is automatically double quoted?

Iā€™ve still got my old answer here. Iā€™m afraid Iā€™m off to bed but Ill leave you the solution I found to each of the sections. They are behind spoiler banners so you donā€™t have to look!

If prop isnā€™t tracks and value isnā€™t an empty string, update or set that albumā€™s prop to value.

    if (prop !== "tracks" && value !== ""){
        console.log("chose 1");
        records[id][prop] = value;
    } 

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

   else if(prop == "tracks" && !records[id].hasOwnProperty("tracks")){
        console.log("chose 2");
        records[id][prop] = [value];
    } 

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

    else if(prop == "tracks" && value !== ""){
        console.log("chose 3");
        records[id]["tracks"].push(value);
    }

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

 else if(value === ""){
        console.log("chose 4");
        delete records[id][prop];
    }

The console.logs were there to let me know where in the chain I was when debugging!

Thank you for your solution but I wonā€™t look at it, I have to try to solve it on my own :slight_smile:

Whatā€™s your latest code? When you make changes, it helps people help you if you show the new code.

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

// Only change code below this line
function updateRecords(records, id, prop, value) {
  if (prop!=="tracks" && value!==0) {
recordCollection.artist = 'value';
recordCollection.tracks = 'value';
  
}

return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

trying to solve one condition by one now.

if there is a parameter value, and the parameter is a string do I need to type as value or as ā€˜valueā€™? itā€™s confusing, but I donā€™t think that is the issue as well.

edit: another question if you can answer please.

damianicely suggested this
Hint 3) donā€™t give yourself unnecessary work
prop!==recordCollection.tracks
has less room for error if insted you try:
prop !== "tracks"

If I want to use an object property inside my statements I have to put the strings?
I know JS does it automatically but I guess itā€™s a different scope?

Two problems here.

  1. You should not hard code the property you are changing.

  2. You should use the variable value instead of the string "value".

Do you remember bracket notation for accessing object properties?

Yeah I remember, you can use either dot or bracket.
and I think I learned that you use bracket if you have a space in the property name is that correct?

edit: what does it mean " hard code " ?

You also need to use bracket notation when you have a variable holding the property name.

ā€˜Hard codeā€™ in this context means that you are writing the code to only work for one (or in this case two) property name instead of using the name stored in the variable prop.

thatā€™s what I dont understand value isnā€™t a property itā€™s just a parameter.

You should only use

You should not use artist, for example, and you really shouldnā€™t use "value".

Understanding what these four parameters mean is the crux of this challenge.

I think I am starting to get what you mean, Iā€™ll look more into it. ty

1 Like

Weā€™ll be here for more questions :slight_smile:

1 Like

hey, I am stuck again

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

// Only change code below this line
function updateRecords(records, id, prop, value) {
  if (prop!=="tracks" && value!==0) {
records[prop] = records[id][prop];
console.log(records[prop]);
}

return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

so it gives me undefined. I have a few questions.
you gave me a hint only to use the parameters. I thought value is any of the key values. I looked at the hint they hinted to use records[id][prop].
I can understand why it is undefined because those are just parameters, they have no connection to the object itself.

I can also understand this ā€œAnother use of bracket notation on objects is to access a property which is stored as the value of a variableā€

but parameters here are just parameters, they are nothing not a value not a variable.
itā€™s very confusing to me any hints ;p?

the parameters get a value when the function is called, so you need to consider what possible values they can have, and make your function act accordingly depending on the input to get the wanted output

careful here, you are changing records and adding a new property with value undefined

I would suggest you use this: JavaScript Tutor - Visualize JavaScript code execution to learn JavaScript online

and add one of the function calls from the tests to see your function execute