Basic JavaScript: Record Collection Please Guide Me

Basic JavaScript: Record Collection Please Guide Me
0

#1
function updateRecords(id, prop, value) {
  
  if (prop != "tracks" && value != "") {

    collection[id][prop].push(value);

  }



  if (value === "") {

    delete collection[id][prop];

  }

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

    collection[id][prop].push(value);

  }

  if (prop === "tracks" && (collection[id].hasOwnProperty(tracks) == false) ) {


collection[id][prop] = [""];
collection[id][prop].push(value);

  }

return collection;

}

The above is what I have so far for this challenge. The challenge link is below:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection

I was sailing past everything and yeah, you git a wall sooner or later!

I keep getting
‘‘Cannot read property ‘push’ of undefined’’
6 times, every time I run the above

I am trying to follow it in the order the question is worded. For instance,

‘‘If prop isn’t “tracks” and value isn’t empty (""), update or set the value for that record album’s property.’’

To satisfy the first request above, I used the following code:


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

    collection[id][prop].push(value);

  }

I don’t even know if this section is incorrect or if it’s another part of the code etc. since the feedback is vague.

What is wrong here? I initially found it was a silly mistake like using one = instead of == or === but correcting this didn’t make any difference. Please give me some pointers, thanks in advance.


#2

Try like this

 if( property === 'tracks' && collection[id][property]){
 collection[id][property].push(value);
 }
 if( property === 'tracks' && !collection[id][property]){
 collection[id][property] = [value]
}

#3

I’ve edited your post 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 easier to read.

Note: Backticks are not single quotes.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#4

Thank you, this fixes some of it

Can you explain what was wrong with mine?


#5

Also

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

    collection[id][prop].push(value);

  }

seemed to be causing one of those ‘‘Cannot read property push as undefined’’ errors so I removed it. Why does this not work?

I have this now:

var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  
  
  if (value === "") {

    delete collection[id][prop];

  }


  if (prop === "tracks" && collection[id][prop] ) {

collection[id][prop].push(value);

  }

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

    collection[id][prop] = [value];
  }


return collection;

The errors given are now:

After updateRecords(5439, "artist", "ABBA"), artist should be "ABBA"
After updateRecords(2548, "tracks", ""), tracks should not be set
After updateRecords(1245, "album", "Riptide"), album should be "Riptide"

Struggling to understand what is wrong atm so any guidance and explanations are welcome


#6

hey you missed the first condition
If prop isn’t “tracks” and value isn’t empty (""), update or set the value for that record album’s property.
add this to your code

if(property !== 'tracks' && value !== ""){
  collection[id][property]  = value;
}