Basic JavaScript - Record Collection - Alternative Solution

Hi there,

i have been scratching my head on this solution. I’ve read some other threads about this exercise, but they all seem to test the prop variable for “tracks”. My first instinct was to make it a bit more flexible.

It seems to resolve most actions, except for the following:

// running test
After updateRecords(5439, “artist”, “ABBA”), artist should be “ABBA”
After updateRecords(1245, “album”, “Riptide”), album should be “Riptide”
// tests completed

Is it at all possible to resolve this my way, or should I test for “tracks” as i have seen other people do.

function updateRecords(id, prop, value) {
  // if the album doesn't have the prop set
  if(!collection[id].hasOwnProperty(prop)){
    //create prop with empty array
    collection[id][prop] = []
    
  };
  // if value is empty 
  if (value == ""){
    //delete the property
    delete collection[id][prop];
    // if value is NOT empty
  } else {
    // push the value on the property array
    collection[id][prop].push(value);
  }
 
  return collection;
}

Ok, after some troubleshooting. i found what I did wrong, I was always pushing the “value” on to an array. I was able to fix it by changing the last part to:

// if value is empty 
  if (value == ""){
    //delete the property
    delete collection[id][prop];
    // if value is NOT empty
  } else if (prop == "tracks") {
    // push the value on the property array if prop is "tracks"
    collection[id][prop].push(value);
  }else {
    collection[id][prop] = value;
}

To get around, directly testing the “prop” variable for “tracks”, I try something like this:

else if (collection[id][prop] === Array)

But this give me the following error:

// running test
updateRecords(…)[5439].tracks.pop is not a function
updateRecords(…)[1245].tracks.pop is not a function
After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.
// tests completed

Is there a way to complete this assignment “my way”? Or should I just leave it be and get on with it? :slight_smile:

Hi,
Where the Array came from?

else if (collection[id][prop] === Array)

I want to check if the property is an array, if not, i cannot use .push. Am i missing something?

You can’t use the strict equality (or non strict) operator to test to see if something is an array. If you are looking to check whether a variable is an array, you can use:

hope this helps :slight_smile:

@oliverdudman is right you should use the Array.isArray() method. Base on the question given I don’t think you need to check if **collection[id][prop] ** is an Array as you’ll set it before to push any value.

For more info on how to check for Array :

https://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript?noredirect=1&lq=1

Hope this help.

Thanks for the replies both, you’ve been a great help!

1 Like

In case you need an insight this is my solution :wink:

// Only change code below this line
function updateRecords(id, prop, value) {
  if(prop !== "tracks" && value !== "") {
      collection[id][prop] = value;
  } else if (prop === "tracks" && value !== "") {
    if(!collection[id].hasOwnProperty('tracks')) {
       collection[id][prop] = []
    }collection[id][prop].push(value)
  } else {
    delete collection[id][prop];
  }
  
  return collection;
}

2 Likes

Thank-you Ephrey. I saw your solution quite a few hours ago (waaaay to many hours), but I wanted to make it work as a more direct translation from the pseudocode. I tried a bunch of different ways to do this without having the if within the else if. The way you did it makes sense, because there are two different situations that can happen if you’re trying to add a track. The first is if you don’t have the prop tracks and the other is if you do. I can’t imagine how anyone did this challenge without doing it this way. If anyone else did it without the if within the else if, could they please post?

Hello, this was the solution I used for this challenge

// Only change code below this line
function updateRecords(id, prop, value){
   
    if (value  === ""){
    delete collection[id][prop];
  }
  else if (prop === "album" || prop === "artist"){
      collection[id][prop] = value;
  }
  else if (collection[id].hasOwnProperty(prop)) {
      collection[id][prop].push(value);
  }
  else collection[id][prop] = [value];
  
  return collection;
  
}