Basic JavaScript: record collection question

Basic JavaScript: record collection question
0.0 0

#1

Hi all, I have a question for future reference relating to the record collection exercise.
Is there a reason why when I tried to add the tracks property to records that didn’t have it at all using just :
collection[id][prop] = value;

It wouldn’t go through (pictured below), but when I make an empty “tracks” property and then push my value into it, it works?

Using:
collection[id][prop] = [];
collection[id][prop].push(value);

I’m a bit confused why the first method worked for other properties like artist and album, but didn’t work for the tracks. TIA for any clarification.

image


#2

You may please provide the code in plain text, same share the challenge link too? this image is not clear. thanks.


#3

Record Collection challenge link

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

#4

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.


#5

This is becasue of

Note challeng excepted the "tracks" be kind of array, rather plain string.

So this code collection[id][prop] = value; set the prop as string, which should be an array.

tip: first initialize the collection[id][prop] as an array, then simply push the value to it

keep goin on great work, happy programming


#6

The problem is the following line:

    } else {
      collection[id][prop] = value;
    }

Remember, the “tracks” property is an array and not just a string. You can either do as @NULL_dev suggests or wrap [ ] around the value which will make it an array with one element (the value).


#7

Well take a look at these lines

    if (collection[id].hasOwnProperty(prop)) {
      collection[id][prop].push(value);
    } else {
      collection[id][prop] = value;
    }

Focus on the else block.

The task asks you this:
To create an empty array, then push the value.

What you wrote is following.
Just set it as value.

So, you are missing the step to create an empty array and putting element to that array when tracks doesn’t exist.

You can slightly change the flow and write something like this:

if ( !collection[id].hasOwnProperty(prop) ) {
    collection[id][prop] = []
}
collection[id][prop].push(value)

#8

Thanks! That makes sense why my second method was correct then. I’ve been arguing with this problem for a while so I just couldn’t see it.


#9

Thanks, I actually solved it with

if ( collection[id].hasOwnProperty(prop) ) {
    collection[id][prop].push(value);
} else{
collection[id][prop] = [];
collection[id][prop] = value;
}

I just didn’t understand why I needed to make the array. The answer was I needed to go back and read the prompt again :sweat_smile:


#10

Last line
collection[id][prop] .push(value);