[Solved]Basic javascript: Record collection :'(

[Solved]Basic javascript: Record collection :'(
0

#1

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/


#2

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.


#4

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.


#5

Reputation? What are you talking about? Clicking the “solved” checkbox helps others know before clicking the thread, that an answer has been found to the question raised by the OP.


#6

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.


#7

I see what you mean now. Even if there was a summary count somewhere it would be meaningless, because users can reply to their own threads and mark those replies Solved.


#8

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.


#9

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: