Basic JavaScript - Record Collection

Tell us what’s happening:
I can not figure out what I am doing wrong . I have tried a few variations of my code, but I seem to keep getting the same failures

Your code so far

// Setup
const 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 === "") {
    return delete records[id][prop]
  }
  else if (prop !== 'tracks' && value !== "") {
    return records[id][prop] = value
  }
  else if (records[id].hasOwnProperty('tracks') === false && prop === 'tracks') {
    return records[id][prop] = [value]
  }
  else if (prop === 'tracks' && value !== "") {
    return records[id][prop].push(value)
  }
  
  return records;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

Take a closer look at what function returns for a couple of test cases:

console.log(updateRecords(recordCollection, 5439, 'artist', 'ABBA'));
console.log(updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"));
console.log(updateRecords(recordCollection, 1245, "albumTitle", "Riptide"));
console.log(updateRecords(recordCollection, 2468, "tracks", "Free"));

That doesn’t seem to be the whole object with records. Any ideas why that’s happening?

Welcome to the FCC community!

This is one of the most difficult JavaScript lessons I encountered.
I read where many people have difficulty with it.
Once you get it, you will feel the great accomplishment of success.

@sanity has provided guidance.

I just wanted to welcome you and let you know, you are not alone with finding this one challenging.

Happy coding! :slight_smile:

1 Like

Hi.
I am still having trouble with it. I tried your test cases, and I do see that I am only getting the value returned. But I can not figure out why

Take a look at function, where are the return keywords, and which of them might not return the whole records object?

Ok. So I took all of the return keywords away from the if/else statement and left the last one “return records” at the end of the function.
That worked, but I do not fully understand why. Could you help me understand why taking the return keyword from my if/else statements made it work please?

Tests are expecting the whole object to be returned. However each of the return in the if/else was not returning the whole object, but whatever the expression by the return was evaluating to.

For example, the push method returns the new length of the array, so return records[id][prop].push(value) was returning integer, instead of the expected object.

oooohhhhh. I see…

Thank you very much

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.