Basic JavaScript: Record Collection - in non-tech terms please!

Basic JavaScript: Record Collection - in non-tech terms please!
0

#1

Hello, can someone please explain - in terms that would be used by a non-tech manager, for example - what the goal of the code should be? So please don’t use any coding terms like array or push or function.

I know that we have a list of albums with data about each album (and some missing data) and that we want to manipulate that data in some way to achieve some end result. But I am getting all knotted up in the tech “trees” of this challenge that I cannot see the “forest”.

And I don’t want to see anyone’s else’s code b/c that will not allow me to independently think this through. I’ll only be reverse engineering from those answers.

Thank you in advance.


Basic JavaScript: Record Collection - why didn't MY code work?
#2

Can you link to the challenge please?


#3

The goal is specified by the challenge instructions. Why don’t you tell us which parts of the instructions are confusing to you? That way, we won’t waste time writing explanations for things you already understand.


#4

Oh, that one. Yeah, that’s really confusing. :confused: :
Okay:

  • They are giving you a list of albums, each with different attributes including a item number.
  • Your function will update the list based on:
    1. A given list number to identify the album (eg: 1242),
    2. The attribute to update (eg: artist),
    3. The new value for the given attribute (eg: ABBA).
  • You are also warned of the following issues:
    1. Make sure the attribute exists. If not, create it first.
    2. If the value is empty, delete the attribute.
  • Warning, the tracks attribute is itself a list.

The reason it’s confusing, I think, is that they’re setting you up for the code. If you convert the instructions to code line by line, it should work.

Hope this helps!


#5

Actual instructions: Write a function which takes an album’s id (like 2548), a property prop (like “artist” or “tracks”), and a value (like “Addicted to Love”) to modify the data in this collection.

How I interpret that:

function function_name(id, prop, value){
     ***code that does stuff***
}

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

How I interpret that:
Find all the non-empty properties that are not “tracks” and…this is where I get confused. So I will end up with a collection of these properties/values:

2548: an object,
album: slippery when wet,
artist: bon jovi,
tracks: let it rock, you give love a bad name

But what do I do with them? The instructions [update or set the value for that record album’s property] seem to suggest I should set these values for those properties…but those values already exist for those properties…??


#6

Your function should update the properties. Not all of them exist.


#7

If the prop argument passed into the function is not “tracks” and the value argument passed into the function is not a blank string, you will need to update or set a value for that album’s property. When you read further down into the instructions, you get more information about what to do if val is blank or not. At this point in the instructions, you just need to understand you will need to do something if those two conditions are true.


#8

OK I looked up the final answer b/c I was just hopelessly lost.

And based on that and the instructions:

  1. we are given 3 values (id, prop, value).

For the given id:

1. if prop <> tracks & value <> "": set that collection.id.prop = value.

2. If prop === tracks but collection.id.tracks does not exist: create collection.id.tracks array and set it to value.

3. If prop === tracks and value <> "":  add value to tracks.

These steps can be streamlined:

IF value <> "":
     AND IF prop === tracks THEN: add value to tracks
     ELSE: set that collection.id.prop to = value
ELSE:
    IF prop === tracks (do I also need to check if collection.id.tracks exists at this point in the IF/THEN code?): 
                                                                                                                                create collection.id.tracks array and set it to value

Is this accurate?


#9

I would reword #2 to be:

If prop === tracks but collection[id].tracks does not exist: create collection.id.tracks array and add value to the array.

Note above I used bracket notation instead of dot notation for id. Why? Because there is no property named id in collection. id is a variable, so to reference a property with a variable you must use bracket notation.

Your streamlined version would be more accurate and concise if written as:

IF value <> "":
     AND IF prop === tracks THEN:
          IF tracks property exists add value to tracks
          ELSE: create tracks array and add value to the array
     ELSE: set collection[id].prop to = value
ELSE:
     Delete prop from the collection                  

#10

Thank you for your help so far.

I converted the pseudocode to real code. Now all but one testcase is passing.

My code:

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

  return collection;
}

Failed test-case:
After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.


#11

Thread continued here >>> Basic JavaScript: Record Collection - why didn't MY code work?


#12

This is a follow-up from a previous thread (here: https://forum.freecodecamp.org/t/basic-javascript-record-collection-in-non-tech-terms-please/224744/9). Apologies if we are not supposed to start new threads but my question has changed slightly since my last post and I figured it would be OK to start a new thread.

This was the last code I tried:

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

return collection;
}

But still this testcase was failing: After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.

So then I subbed in the code from Basic Code Solution:

if(collection[id][prop])

instead of

if (collection.hasOwnProperty[prop] === true){

I get why the FCC solution worked but I don’t understand why my code was not acceptable?

Thanks!