Record Collection How to solve

Record Collection How to solve
0

#1

Tell us what’s happening:
if(value==“free”){
return collection;
}
why is my code not working here
Your code so far


// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
 function updateRecords(id, prop, value) {
   if(value==""){
     delete collection[id][prop];
   }
   else if(prop!=="tracks"&&value!=="") {
      collection[id][prop] = value;
   }else if(prop=="tracks"&&value!==""){
     if(value=="free"){
       return collection;
     }else{
     collection[id][prop]=[];
     collection[id][prop].push(value);
     }
   }
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection


#2
if(value=="free")

Can you explain what you are trying to do here, why you are checking this specific value, and why you are just returning the collection when that check returns true (which it never will for any test)?


#3

Hello @GUILL1203

First of all, be careful with else ifs statements. You should set priorities: A code in else if that has more priority to run than another one contained in an if / else if statement, if it’s put in the wrong place, it will not run.

Also, don’t forget to read this carefully: …but the album doesn't have a "tracks" property… which is not correctly implemented in your else if.

And for your information, the if which checks for value=="free" is unnecessary. Why did you add it?

And finally, you would better create a condition for the following rather than putting it into a vague else so that you’ll be sure you get the right results.


#4

All the other tasks in this challenge have passed, and this one remains
:After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.


#5

tracks is an array.

  1. If tracks already exists, then push the new track into the array.
  2. If it doesn’t exist, create the array and push the new track into the array.

The thing is, you’ve done the second option. I’m assuming you’ve copied from somewhere, because if you understood why that was there you’d see why what you’ve written makes no sense.

What that test is checking is that you are pushing new tracks into the array.


#6

I adjusted the structure but it still didn’t work?

function updateRecords(id, prop, value) {
if(prop==“tracks”&&value!==""){
if(value==“free”){
return collection;
}else{
collection[id][prop]=[];
collection[id][prop].push(value);
}
}
else if(prop!==“tracks”&&value!=="") {
collection[id][prop] = value;
}else if(value==""){
delete collection[id][prop];
}
return collection;
}


#7

Stop checking for free:

a. “free” isn’t a value that exists in any of the tests
b. “Free” is a test value, there to check that your logic is correct. It’s an example, and it could be anything, you don’t hardcode it into your own code.
c. Explain what the lines after are doing (the ones that create an array and push to it). If you can understand why you’ve written them, then the thing you’re missing should be obvious.


#8

If I don’t think about prop=“tracks” and value=“free”, wouldn’t I add “free” to the last item in the array tracks?


#9

Ok, firstly it isn’t “free”, that value only exists in the codeyou have written, nowhere else.

Secondly, here is what, logically, I would expect this function to do:

Given the ID of an album, if I want to update the tracklist and I have the name of a song, then add that song to the tracks and return the updated collection

Your logic is:

Given the ID of an album, if I want to update the tracklist and I have the name of a song, then if the name of the song is “free” return the collection as-is (ie don’t do anything). If the name of the song isn’t “free”, just don’t do anything


#10

Look, you need to follow what the challenge is instructing you to do.

The challenge didn’t ask you to check for value == "free".

Let’s break the problem down into smaller parts to make it easier:

  1. You have a function, it receives 3 arguments: number (id), the name of the property, value.
  2. If prop is not the array tracks, and the value passed to the function is not empty, update the value in the targeted object.
  3. Return the entire collection object.

Great! If you did these, you did a good job!

What you also need to do is to check whether data is incomplete:

  1. What if prop is "tracks", but the targeted album doesn’t have property tracks? create an empty array, then push into it the value.

  2. What if prop is "tracks" and value isn’t empty? push the value to the array tracks.

  3. If value is empty, delete the given property!

I basically said the same thing said in the challenge, but here’s a bonus:

What should you start with FIRST?

Will you go and update your objects?

OR

Check for incomplete data first, then update the object?

It’s up to you, now, to apply all of these.

And yeah, it’s very important to understand the challenge in order to get to the solution quickly and easily.

Don’t hesitate to ask more questions.


#11

Ok, thank you, maybe I’m a little confused here, so I’m going to sort this out.I believe I can solve this problem well


#12

Alright, I hope you will do it

But here’s a refresher:

  1. What is an argument: Passing Values to Functions with Arguments.

  2. What is a property of an object: Create a Basic JavaScript Object.

  3. How to target/access properties in an object:
    -Use Dot Notation to Access the Properties of an Object.
    -Accessing Object Properties with Bracket Notation.
    -Accessing Object Properties with Variables.

  4. How to update a property in an object: Updating Object Properties.

  5. Deleting a property from an object: Delete Properties from a JavaScript Object.

  6. Check whether a property exists in an object: Testing Objects for Properties.