Record collection exercise help

Can someone please explain why the tests are not passing? thank you :slight_smile:

  **Your code so far**

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

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/91.0.4472.124 Safari/537.36 Edg/91.0.864.67

Challenge: Record Collection

Link to the challenge:

do not reference the global variable, use the function parameters

2 Likes

Why do we do that? Isnt the data already in the album meant to be referred to and not the parameters? I dont quite understand

If you hard code that your function is only going to use the global variable, then you cannot use any other data inside of the function. We want flexible functions.

your function should be reusable, you made so that it works only with that specific object, and ignored one of the function parameters

Which parameter did I ignore? So your saying that the recordCollection is the global variable, and if I use it within my function again, it cant be used outside of my function? So i should use my parametersā€¦?

function updateRecords(records, id, prop, value) {

if (prop !== ā€œtracksā€ && value !== ā€œā€){

return records[id][prop] = value;

} else if (prop === ā€œtracksā€ && records[id].hasOwnproperty(ā€œtracksā€) === false){

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

} else if (prop === ā€œtracksā€ && value !== ā€œā€){

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

} else if (value === ā€œā€){

delete records[id][prop];

}

return records;

}

updateRecords(recordCollection, 5439, ā€˜artistā€™, ā€˜ABBAā€™);

this is my code now but I still cant seem to pass the tests

Imagine you have two record collections on your computer, one for pop and one for punk. Those two are global variables, so you want to make sure that your function only works on the collection that you pass in as parameter. The function does its stuff and returns the updated collection. The other collection remains untouched.

1 Like

now it may just be you need to check how the method is written - JavaScript is case sensitive

1 Like

I see, but how does the function know which record to access? Your saying by the parameter? How does the parameter know which word its referring to? Like dont you need to declare that as a variable or something? Sorry, I am quite new to programming.

1 Like

So you have two collections, and one function to update either of those collections.

var collectionPop = {
  1234: {
    artist: 'Jon Bohn Jovi',
    ...
  }
}

var collectionPunk = {
  2345: {
    artist: 'Angelic Upstarts',
    ...
  }
}

// this updates only the pop collection
updateRecords(collectionPop, 1234, 'tracks', '1999')

// this updates only the punk collection
updateRecords(collectionPunk, 2345, 'tracks', 'Police Oppression')

The idea of a (reuseable) function is that you

  • give it some input
  • the function does stuff to the input (and only to the input, not something that exists outside of it)
  • the function returns some output

Note how inside the function, youā€™re never manipulating var recordCollection directly, you only manipulate the argument records that was passed in.

Donā€™t worry if you donā€™t get it right away, itā€™ll sink in as you proceed.

1 Like

That actually helped a lot, thank you. So by adding collectionPop as a parameter in that example, does that allow it to be reused even outside the function? Or notā€¦?

and are all the parameters in the same function somehow linked to one another, like as in a particular order from left to right

the parameters in a function take the value from the arguments when the function is called

the first argument gives value to the first parameter, the second argument to the second parameter and so on

1 Like

Thank you that helped a lot

The collection objects arenā€™t reusable, they always hold the current state of your collection data. When you update a collection, you reassign the variables collectionPop or collectionPunk to new values, so those variables now hold the updated collection.

var collectionPop = {...}
var collectionPunk = {...}

// now you update your pop collection

var collectionPop = updateRecords(collectionPop, 3456, ..., ...)

The function returns a new collection object. This new object is now stored in collectionPop, it has replaced the old object that was stored there before.

The word ā€œresusableā€ means that your function works for both collection. You could have hundreds of collections, but you only need one function to update any of them. The function knows which collection to update, because you pass it in:

updateRecords(collectionPop, 3456, ..., ...)

All other collections remain completely untouched.

1 Like

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