[Solved]Basic javascript: Record collection :'(

I was doing the Record Collection challenge in Basic Javascript, my code completes 6 of the 7 conditions. I have checked the hints and understood the solution given there. But as I was doing it in a different way, I couldn’t understand why the 2nd condition isn’t fulfilled. can anyone help to fix me my code? My code is given below:

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

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

I ran your code in that challenge, and the error I was getting in the FCC console was : “updateRecords(…)[5439].tracks.pop is not a function”

I inserted some console-debugging. Insert it into your code

  console.log("id:"+id+", prop:"+prop+", value:"+value);
  console.log("state of collection object after code:");
  console.log(collection);

  return collection;
}

I suggest you paste the debug statements into your code, and look at the browser console (F12) to see what happened. You will learn more that way. If you still can’t figure it out after that, unblur the spoiler below:

Since pop() is a function of arrays, I wondered if you weren’t following the instructions that said: “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.” The console debugging proves that you didn’t.

Hi dude, if my answer helped, click the “solved” checkbox. It increases my reputation. You don’t need to withdraw the post when your question is answered.

Sorry, I’ve been on stackoverflow too much recently. I meant something like this: https://forum.freecodecamp.org/u/randelldawson/activity/solved, but I realize now that it doesn’t get counted in any way, just listed.

Hm. I never thought of the potential for abuse. I know that in Stackoverflow you can’t vote on your own questions/answers/comments. Maybe when I get better I can work on something like that. You’d have a rep through the roof, man.

I found the defect in my code. It was supposed to be (prop === "tracks), but i wrote (collection[id][prop] === "tracks") .
So the code should be the following:

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

But thanks anyway for your advice :slight_smile:

1 Like