Record Collection// well, at least I am still trying!

Record Collection// well, at least I am still trying!
0

#1

Tell us what’s happening:
I’m having good amount of difficulty understanding the instructions. I read several topics created and their solution.
I can’t say I have perfect understanding of the problem yet.

now after I wrote and corrected almost the whole thing based on what I found in other ppl solutions, I still get error
#error
After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.

I’m not sure if I’m on the right track. I don’t have any coding background and I wonder if just working with freecodecamp is good enough??
plz advise me .
tnx.

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 (prop!=="tracks"&&value!==""){
    collection[id][prop]=value;
  } 
  else if (prop==="tracks"&&collection[id].hasOwnProperty["tracks"]){
    collection[id][prop]=[];
    collection[id][prop].push(value);
  }
  
   else if(prop==="tracks"&&value!=="")
  {collection[id][prop].push(value);}
  
  

  else if (value===""){
    delete collection[id][prop];
  }

  
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15.

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


#2

You are currently doing this:
collection[id][prop]=[];
when prop ==="tracks" and collection[id] has member "tracks"

Did you mean to empty the array when that is true? I’m guessing not

Edit to fix grammar


#3

would you please explain more?


#4

In the question, it asks you to create an empty array for tracks if tracks isn’t found in the object

Instead, you’ve set the (possibly already nonempty) tracks list to be empty, whenever the tracks property exists!


#5
else if (prop==="tracks"&&!collection[id].hasOwnProperty["tracks"]){
    collection[id][prop]=[];
    collection[id][prop].push(value);
  }

I fix this and 2 new errors show up
//
After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.
After updateRecords(2548, “tracks”, “”), tracks should not be set


#6
  else if (prop==="tracks"&&collection[id].hasOwnProperty["tracks"]){
    collection[id][prop]=[];
    collection[id][prop].push(value);
  }
  
   else if(prop==="tracks"&&value!=="")
  {collection[id][prop].push(value);}

You have the logic of these backwards.


#7

for the 1st, it should solve this
//If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.

for the 2nd, I think it goes with this
//If prop is “tracks” and value isn’t empty (""), push the value onto the end of the album’s existing tracks array.


#8

You’re doing the opposite.
collection[id].hasOwnProperty["tracks"] is true if the album DOES have the tracks property. Then you replace it with an empty array.

That means that your next else if only runs if the album DOES NOT have the tracks property. You then attempt to push to an undefined value, which causes an error.


#9

ok… sorry if I seem so dumb…

now here is what I understood

  else if (prop==="tracks"&&collection[id].hasOwnProperty["tracks"]){
    
    collection[id][prop].push(value);
  }
  
   else if(prop==="tracks"&&value!=="")
  { collection[id][prop]=[];
  collection[id][prop].push(value);}
  

but I end with this error

After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element


#10

hasOwnProperty is a function. You must use parentheses ( ) when calling a function. You are using square brackets [ ] which is not valid syntax.

Also, you need to learn how to properly indent and space your code, so that it is more readable. Instead of:

  else if (prop==="tracks"&&collection[id].hasOwnProperty["tracks"]){
    
    collection[id][prop].push(value);
  }
  
   else if(prop==="tracks"&&value!=="")
  { collection[id][prop]=[];
  collection[id][prop].push(value);}
  

write it:

  else if (prop === "tracks" && collection[id].hasOwnProperty("tracks")) {
    collection[id][prop].push(value);
  }
  else if (prop === "tracks" && value !== "") {
    collection[id][prop] = [];
    collection[id][prop].push(value);
  }

#11

ok. thank you very much. I’ll work in the spacing.

so I have this error left to pass,

After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.


#12

I would need to see the full code to comment.


#13
function updateRecords(id, prop, value) {
  
  if (prop!=="tracks"&&value!==""){
    collection[id][prop]=value;
  } 
  else if (prop === "tracks" && collection[id].hasOwnProperty("tracks")) {
    collection[id][prop].push(value);
  }
  else if (prop === "tracks" && value !== "") {
    collection[id][prop] = [];
    collection[id][prop].push(value);
  }
  

  else if (value===""){
    delete collection[id][prop];
  }

#14

I will assume you accidentally left off the remainder of your function such as:

  return collection;
}

If so, then when I run your code code above with the missing function code I referenced above, the only error I get is:

After updateRecords(2548, “tracks”, “”), tracks should not be set

If you are getting a different error than I show above, you must be running different code.


#15

yes, correct. I’m getting the same error.


#16

I just wanted to make sure, because you said you were getting a different error in the previous post.

The instructions state “If value is empty (”"), delete the given prop property from the album.". That means since the value argument is a blank string, your code should be deleting the tracks property from record “2468” after the above test runs. Unfortunately, it does not do that, hence the failing test.


#17
else if (value===""){
    delete collection[id][prop];
  }

isn’t this code supposed to cover this problem?


#18

It would if the else if (seen below) before the else did not evaluate to true. In the test, your code adds a blank string element to the end of the tracks array.

  else if (prop === "tracks" && collection[id].hasOwnProperty("tracks")) {
    collection[id][prop].push(value);
  }

#19

so it should evaluate to false?

and when i delete this else if, I end up with this error,
After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.


#20

Instead of deleting/modifying/adding new code, I suggest you write out your algorithm for this challenge first in plain language. Write down the exact steps you would take if you were the computer. That means you need to make sure your logic will work on paper with each test case before writing ANY code. At this point you are just guessing at a solution by changing bits of code here and there, instead of understanding what the algorithm should be for you to be able to write the correct code.

An algorithm is just the logical step by step process you need to perform to get to the correct response.