Stuck on the Record Collection Challenge

Link to challenge: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection

Hi, below is my attempt and I couldn’t pass the test. If anyone can point out my mistakes I will be appreciated. Btw, I looked at the solution already and my code is similar to solution 2, except I avoid the OR operator since short-circuit evaluation has not been taught to me.

// Setup
var recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
// If value is blank, delete the prop property
    if (value === ' ') {
      delete recordCollection[id][prop];
    } else {
// If value is not blank, prop is tracks, and the tracks array exists, add value to the array
      if (prop === 'tracks' && recordCollection.id.hasOwnProperty("tracks")) {
        recordCollection[id][prop] = [value];
        } else {
// If value is not blank, prop is tracks, but no tracks property, create an array and add value to it.
          recordCollection[id][prop].push(value);
        }
      } else {
// If value is not blank but prop isn't tracks, set prop to value.
      recordCollection[id][prop] = value;
      }
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Two problems here.

  1. You should not reference the global variable

  2. You cannot use dot notation with a variable holding the property name

Also, you are missing any sort of return statement.

1 Like

Hi, you made me realize recordsCollection is the global variable so I changed that to just records. To address your point #2, I changed recordCollection.id.hasOwnProperty(“tracks”) into records.id.hasOwnProperty(“tracks”), is that correct? And one more question: I try to evade the OR operator but I don’t know how (maybe there isn’t a way to do that?). I’m sure the way I’m doing here is incorrect (see the second if statement).

// Setup
var recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
// If value is blank, delete the prop property.
    if (value === ' ') {
    delete records[id][prop];
    } else {
// If value is not blank, prop is tracks, and the tracks array exists, add value to the array.
      if (prop === 'tracks' && records[id].hasOwnProperty("tracks")) {
      records[id][prop] = [value];
      } else {
// If value is not blank, prop is tracks, but no tracks property, create an array and add value to it.
        records[id][prop].push(value);
// If value is not blank but prop isn't tracks, set prop to value.
        records[id][prop] = value;
       }
  return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

This looks like you are overwriting the array if it exists here.

And here you are first pushing and then overwriting the array with a single value.

you are still using dot notation to access a property with a variable, which doesn’t work

Oops that’s a typo, I did have records[id].hasOwnProperty(“tracks”) in my code.

I don’t understand what you mean by “overwriting the array.” I do know the following

// If value is not blank, prop is tracks, but no tracks property, create an array and add value to it.
        records[id][prop].push(value);
// If value is not blank but prop isn't tracks, set prop to value.
        records[id][prop] = value;

is wrong, because if prop is equal to tracks but doesn’t have the tracks property, the second if statement will executes the else statement, which is both creating an array and set prop to value. I just code like that cause I don’t know where else to put those statements.

this one execute if the object has the property, you are overwriting the existing array with that

Do you mean

      records[id][prop] = [value];

overwrites the existing array? so should it be

        records[id][prop].push(value);

?

it depends on what your intention are, if you want to add an item to an array, the first one doesn’t do that, it creates a new array, the second one add an item to the array, yes

Well my intention is to add an item since that’s what the challenge wants, I just confused adding item with creating an array. Also this part in my code

// If value is not blank, prop is tracks, and tracks array exists, add value to the array.
      if (prop === 'tracks' && records[id].hasOwnProperty("tracks")) {
      records[id][prop].push(value);     
      } else {
// If value is not blank, prop is tracks, but no tracks property, create an array and add value to it.
        records[id][prop] = [value];
// If value is not blank but prop isn't tracks, set prop to value.
        records[id][prop] = value;

is where I want to mimic the following part in solution #2 where

else if (prop === 'tracks') {
    records[id][prop] = records[id][prop] || []; // this is called shortcircuit evaluation, see below for explanation
    records[id][prop].push(value);
  } else {
    records[id][prop] = value;

Is there a way to do that without using the OR operator?

Ignore the solution. Think about your goals.

You have three different cases

  1. updating tracks but tracks does not exist
  2. updating tracks and tracks does exist
  3. updating something other than tracks

You are mixing together these three cases into two cases. You can combine 1/2 if you are careful, but you can’t mix and match 1/2 with 3.

1 Like

I understand I can’t mix 1/2 with 3 because 3 belongs to the non-track category and 1/2 belongs to the track category. I 'm just confused on how to write the code. My problems are: even though 1/2 are in the track category, there is one difference between the two and that is whether tracks property exists or not, as well as where to put the code if prop is not tracks.
Below is what I tried:

      if (prop === 'tracks') {
        if (records[id].hasOwnProperty("tracks")) {
        records[id][prop].push(value);     
        } else {
        records[id][prop] = [value];
        }
      } else {
        records[id][prop] = value;
      }

Explanation on how I did it: the statement below the second if executes if prop is tracks and the property exists. The one below the first else executes if prop is tracks but the property does not exist. The one below the second else executes if prop is not track.
Once I done that, I get the error message at the last line of code that looks like this

 updateRecords(recordCollection, 5439, 'artist', 'ABBA');
                                                          ^                                  

when it points after the last character you are missing a ) or } somewhere

2 Likes

That logic should work but there is some syntax bug.

Ok thanks, I’m almost done. I passed 5 of the 7 tests, the two that I didn’t pass are:

  • After updateRecords(recordCollection, 2548, "artist", "") , artist should not be set

  • After updateRecords(recordCollection, 2548, "tracks", "") , tracks should not be set

what’s your updated code?

// Setup
var recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

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

updateRecords(recordCollection, 5439, 'artist', 'ABBA'); }

when do you want to delete the property?

When value is an empty string, hence ( value === ' ' ) ?