Really need help with record collection problem

Really need help with record collection problem
0

#1

Hey guys I’m struggling a bit with this problem. In the code solution for this particular problem, where is he creating the array, when the prop is ‘tracks’ but the object that is being updated doesn’t have a ‘tracks’ array property? And what condition checks to see if the object doesn’t have a ‘track’ prop? Heres his solution:

function updateRecords(id, prop, value) {
  if (prop === "tracks" && value !== "") {
   if(collection<a href='https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:"' target='_blank' rel='nofollow'>id][prop]) {
    collection[id][prop].push(value);
   }
   else {
    collection[id][prop]=[value];
   }
  } else if (value !== "") {
    collection[id][prop] = value;
  } else {
    delete collection[id][prop];
  }

  return collection;
}

Also here’s my solution, I am failing one test— After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.—

function updateRecords(id, prop, value) {
  if (prop !== 'tracks' && value !== '') {
    collection[id][prop] = value;
  }
  else if (prop === 'tracks' && value !== '') {
   collection[id][prop].push(value);
  }
  else if (value !== '') {
    collection[id][prop] = [value];
  }
  else delete collection[id][prop];

  return collection;


#2

You will notice that the original collection object # 5439 does not have a “tracks” property. After the test case updateRecords(5439, “tracks”, “Take a Chance on Me”) runs, this object should have a tracks array with only one element (the string “Take a Chance on Me”).

Your code below is what attempts to push a value into an array, but since it does not exist yet, your code creates a TypeError, because you can not push something into nothing.

How to resolve? You could create an additional check to see if the tracks prop already exists (think hasOwnProperty method). If it does not exist, then assign an array to a “tracks” property of the object and then push into string value into it. Otherwise, you can use your existing code to push to the existing “tracks” array.


#3

I don’t know what I’m doing. I tried what you said but my code still doesn’t work.

function hasTrackProp(id) {
  if(collection[id]['track'] == undefined) {
    return true;
  }
}

function updateRecords(id, prop, value) {
  if (prop !== 'tracks' && value !== '') {
    collection[id][prop] = value;
  }
  else if (prop === 'tracks' && value !== '') {
   if(hasTrackProp(id)) {
     var newArray = [0];
     var updatedArray = newArray.push(value);
     collection[id][prop].push(updatedArray);
   }
   collection[id][prop].push(value);
  }
  else if (value !== '') {
    collection[id][prop] = [value];
  }
  else delete collection[id][prop];

  return collection;
}


#4

You are not understanding what the following line is doing:

var updatedArray = newArray.push(value);

Read about what gets returned by the push method, so you can understand what the value of updatedArray is after this line is executed.

Also, why are you assigning an array with the value 0 in it? It should be an empty array.

Your hasTrackProp function could be replaced by the hasOwnProperty method in your if statement:

if(!collection[id].hasOwnProperty('tracks')) {

Lastly, if the ‘tracks’ property did not exist and your if block of code adds the correct EMPTY array and then correctly pushes the value into the ‘tracks’ array, the line after your if block of code will push the same value again. You only want to push value one time. My suggestion is to take the push out of the if block of code since the line following will take care of that for you. The if block of code only needs to create the empty array if needed.


#5

Thank you so much I finally solved it, I’m gonna pick this one apart till I understand it completely. Starting with:

collection[id][prop] = value; VS collection[id][prop] = [value]; ----what’s the difference?

also is collection[id][prop] the same as collection[id].[prop] or collection.id.prop?

I guess I can tweak the code and see, but thanks again, you helped a lot!


#6

If value is a string or number, then the first just assigns the string or number to collection[id][prop] and the second assigns an array with value being the first element in it.


#7

oh ok, heres my solution that worked

function updateRecords(id, prop, value) {
  if (prop !== 'tracks' && value !== '') {
    collection[id][prop] = value;
  }
  else if (prop === 'tracks' && value !== '') {
   if(!collection[id].hasOwnProperty('tracks')) {
     var newArray = [];
     newArray.push(value);
     collection[id][prop] = newArray;
   }
  else collection[id][prop].push(value);
  }
  else if (value !== '') {
    collection[id][prop] = [value];
  }
  else delete collection[id][prop];

  
  return collection;
}

Though i noticed when I take out that last if else statement, making the code:

function updateRecords(id, prop, value) {
  if (prop !== 'tracks' && value !== '') {
    collection[id][prop] = value;
  }
  else if (prop === 'tracks' && value !== '') {
   if(!collection[id].hasOwnProperty('tracks')) {
     var newArray = [];
     newArray.push(value);
     collection[id][prop] = newArray;
   }
  else collection[id][prop].push(value);
  }
  else delete collection[id][prop];

  
  return collection;
}

It still works, why is this piece of code unnecessary. I only have it in there because it was in the solution that they gave me.


#9

Maybe if you indent your code properly, you will see what is going on better.

function updateRecords(id, prop, value) {
  if (prop !== 'tracks' && value !== '') {
    collection[id][prop] = value;
  }
  else if (prop === 'tracks' && value !== '') {
    if (!collection[id].hasOwnProperty('tracks')) {
      var newArray = [];
      newArray.push(value);
      collection[id][prop] = newArray;
    }
    else
      collection[id][prop].push(value);
    }
  else
    delete collection[id][prop];
  return collection;
}