Help, need to figure out what is wrong in my code regarding -Record Collection

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

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

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

return collection;
  }

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

1 Like

You’re currently saying (for example) 5439.hasOwnProperty('artist') which doesn’t really make sense - 5439 is a number, it doesn’t have a property called artist.

Changing that to collection[id].hasOwnProperty(prop) will make the tests pass: you would now be checking that an object with that ID has a prop, not just a number.


This isn’t how you solve this though: what you’ve done is write code specifically for the seven tests, it’s useless for anything else. You haven’t really solved the problem - those tests are there as a minimal set of checks to ensure that the code works as expected. You don’t code exactly to the values in them, you build a solution then check the tests pass for that solution. You might as well have just used an if block and returned the exact values that are needed to pass the tests, like:

updateRecords(id, prop, value) {
  if (id === 5439 && prop === "artist" && value === "ABBA") {
    collection[5439].artist = "ABBA";
  } else if ....
  }
  return collection;
}

The point is to accept any id, and any prop (though as tracks is the only one that is an array, you can check specifically for that), and any value. If I add another 10, 000 records into the collection, you can’t just get a list of this IDs and then duplicate that if...else block 10,000 times for each one.

One more thing my code is passing for each test case except After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element. I am getting error --After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.Any idea about that

Yes, if you read what I posted. It is, as I said, because you have this line: id.hasOwnProperty(prop) throughout your code, and it makes no sense (id is a number)

changing it as suggested makes the tests pass, but the solution is still seriously flawed for the reasons I outlined (it doesn’t work beyond a tiny set of test cases)

i didn’t notice that before , now it worked , thanks for pointing out.