Basic JavaScript - Record Collection

Tell us what’s happening:
Well, I tested “my code so far” in VS Code and retested for all the errors but coud not replicate them. The function seems fine. So what is going on?

My test protocol below



error list

updateRecords(recordCollection, 5439, “artist”, “ABBA”);
artist should be the string ABBA

RESULT:

[Running] node “f:\data\obsidian\vault_scripts\mymy.js”
{
‘1245’: { artist: ‘Robert Palmer’, tracks: },
‘2468’: {
albumTitle: ‘1999’,
artist: ‘Prince’,
tracks: [ ‘1999’, ‘Little Red Corvette’ ]
},
‘2548’: {
albumTitle: ‘Slippery When Wet’,
artist: ‘Bon Jovi’,
tracks: [ ‘Let It Rock’, ‘You Give Love a Bad Name’ ]
},
‘5439’: { albumTitle: ‘ABBA Gold’, artist: ‘ABBA’ }
}

[Done] exited with code=0 in 0.057 seconds


updateRecords(recordCollection, 5439, “tracks”, “Take a Chance on Me”);
tracks should have the string Take a Chance on Me as the last and only element.

RESULT:

[Running] node “f:\data\obsidian\vault_scripts\mymy.js”
{
‘1245’: { artist: ‘Robert Palmer’, tracks: },
‘2468’: {
albumTitle: ‘1999’,
artist: ‘Prince’,
tracks: [ ‘1999’, ‘Little Red Corvette’ ]
},
‘2548’: {
albumTitle: ‘Slippery When Wet’,
artist: ‘Bon Jovi’,
tracks: [ ‘Let It Rock’, ‘You Give Love a Bad Name’ ]
},
‘5439’: { albumTitle: ‘ABBA Gold’, tracks: [ ‘Take a Chance on Me’ ] }
}

[Done] exited with code=0 in 0.058 seconds

updateRecords(recordCollection, 2548, “artist”, “”);
artist should not be set

RESULT:

[Running] node “f:\data\obsidian\vault_scripts\mymy.js”
{
‘1245’: { artist: ‘Robert Palmer’, tracks: },
‘2468’: {
albumTitle: ‘1999’,
artist: ‘Prince’,
tracks: [ ‘1999’, ‘Little Red Corvette’ ]
},
‘2548’: {
albumTitle: ‘Slippery When Wet’,
tracks: [ ‘Let It Rock’, ‘You Give Love a Bad Name’ ]
},
‘5439’: { albumTitle: ‘ABBA Gold’ }
}

[Done] exited with code=0 in 0.057 seconds

updateRecords(recordCollection, 1245, “tracks”, “Addicted to Love”);
tracks should have the string Addicted to Love as the last element.

RESULT:

[Running] node “f:\data\obsidian\vault_scripts\mymy.js”
{
‘1245’: { artist: ‘Robert Palmer’, tracks: [ ‘Addicted to Love’ ] },
‘2468’: {
albumTitle: ‘1999’,
artist: ‘Prince’,
tracks: [ ‘1999’, ‘Little Red Corvette’ ]
},
‘2548’: {
albumTitle: ‘Slippery When Wet’,
artist: ‘Bon Jovi’,
tracks: [ ‘Let It Rock’, ‘You Give Love a Bad Name’ ]
},
‘5439’: { albumTitle: ‘ABBA Gold’ }
}

[Done] exited with code=0 in 0.056 seconds

updateRecords(recordCollection, 2548, “tracks”, “”);
tracks should not be set

RESULT:

[Running] node “f:\data\obsidian\vault_scripts\mymy.js”
{
‘1245’: { artist: ‘Robert Palmer’, tracks: },
‘2468’: {
albumTitle: ‘1999’,
artist: ‘Prince’,
tracks: [ ‘1999’, ‘Little Red Corvette’ ]
},
‘2548’: { albumTitle: ‘Slippery When Wet’, artist: ‘Bon Jovi’ },
‘5439’: { albumTitle: ‘ABBA Gold’ }
}

[Done] exited with code=0 in 0.058 seconds

updateRecords(recordCollection, 1245, “albumTitle”, “Riptide”);
albumTitle should be the string Riptide

[Running] node “f:\data\obsidian\vault_scripts\mymy.js”
{
‘1245’: { artist: ‘Robert Palmer’, tracks: , albumTitle: ‘Riptide’ },
‘2468’: {
albumTitle: ‘1999’,
artist: ‘Prince’,
tracks: [ ‘1999’, ‘Little Red Corvette’ ]
},
‘2548’: {
albumTitle: ‘Slippery When Wet’,
artist: ‘Bon Jovi’,
tracks: [ ‘Let It Rock’, ‘You Give Love a Bad Name’ ]
},
‘5439’: { albumTitle: ‘ABBA Gold’ }
}

[Done] exited with code=0 in 0.057 seconds



End of test protocol

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 === "") {
    delete recordCollection[id][prop];
  } else if (prop === "tracks") {
    recordCollection[id][prop] = recordCollection[id][prop] || [];
    recordCollection[id][prop].push(value);
  } else {
    recordCollection[id][prop] = 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/111.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

Your function is also returning records after mutating recordCollection. Technically, records is a reference copy of the original recordCollection object passed to the function.

console.log(
  JSON.stringify(updateRecords(recordCollection, 5439, 'artist', 'ABBA')) ===
    JSON.stringify(recordCollection)
); // true

But that is still a very confusing way of working with the data.

If the data structure, function definition, and function call were not located so close to each other in the code you would have a much harder time understanding what was going on in that function.

As said, use the function parameters, that is what they are there for.

Thank you very much for your replies :slight_smile: Much obliged.

I feel, we’re we’re talking past each other.

preliminary remark:

I use the JSON-functions only to get a feeling for what they actually do. They’r an embelishement of console.log() for me.

Actual Situation

  • When I run my code in VS Code, it runs just fine.
    *When I run the very same in the “challeng” window, I get a lot of error messages.

Each of those messages I checked individually in VS Code, and the criticized criteria are without exception met.

So: how come?
Thank you.

Because you are changing recordCollection instead of records. You must not reference the global variable inside of your function

recordCollection is passed as an argument to the records function parameter. It is that records parameter you are supposed to use. You make the changes to that and return that.


What you are doing is mutating the top-level recordCollection object and because the records function parameter is just a reference to that object you can change the top-level object and that gets reflected in the records parameter. So when you return the records parameter it has the same mutations as what was done to the top-level recordCollection object.

As you can see here, we never touch the user object inside the function but it is still changed through the reference to it.

const user = {
  name: 'John',
  age: 30,
}

function setAge(userObject, age) {
  userObject.age = age;
}

setAge(user, 40);
console.log(user); // { name: 'John', age: 40 }

Thank you very much - what an awesome place to ask questions. This obviously is one of the rare places where people actually answer your questions!

1 Like

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