Basic JavaScript - Record Collection

Tell us what’s happening:
Describe your issue in detail here.

Anyone who can explain this better ?

  **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) {
return records;
}

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




  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6 Safari/605.1.15

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

What parts of the instructions have you confused? It’s easier to explain if you point us at what parts are confusing.

Don’t know what to do in this situation.

I don’t know where to start explaining.

How much of the JavaScript curriculum have you completed?

on basic javascript at 93/113

I have completed all the exercises up to Record Collection and have the same problem. Neither the instructions nor the hint solutions make much sense. Is the update function assigning new names id, prop, and value to the number, title, artist and tracks, or should we replace the first with the second? Can anyone explain what is required in plain English, please?

It is pretty plain English already. But perhaps if you can just focus on asking one specific question at a time…

Here is the first paragraph of the instructions:

You are given an object literal representing a part of your musical album collection. Each album has a unique id number as its key and several other properties. Not all albums have complete information.

Is this part fully understood?
(The object literal has some information missing… just like say if you were to write a list of all your favourite songs. You may remember the name but not the singer. Or you may remember the singer but not all the songs etc)

Thank you. How does the code know what id and prop are? Do we need to set const records to [id][prop][value] at the beginning?

At the moment I can’t even get anything when I press run, EV with the simplest of the hint solutions. I’m very new to coding I’m afraid.

The next paragraph may answer you.

Here it is:
You start with an updateRecords function that takes an object literal, records , containing the musical album collection, an id , a prop (like artist or tracks ), and a value .

The code “updateRecords” is a function we are told.
It accepts some inputs which are id , a prop (like artist or tracks ), and a value

So the code gets these from whomever is calling updateRecords (in this instance the test suite will be doing most of the calling but you can also call it in the same manner as the example given to you on the very last line… updateRecords(recordCollection,5439,’artist’,’ABBA’)

Yes, I can see the object literal. My initial question is whether to rename it as ‘records’ to match the question.
Then I need to understand how the later code recognises the ‘id’ number which is different for each record in the array.
Then whether id, prop, value need to be established as an array template or whether JS somehow recognises the switch items with prop, value or with albumName, artist and tracks.
Hope that’s not too confusing.

No. Do not rename the global variable. It is outside of the area you are supposed to edit, according to the comments. records is the function argument. You need this function to work for any record collection object, not just the single global object you were given.

What is the id? What data type? Can you console.log the function arguments?

I don’t understand this question. You have function arguments. The values of those arguments are determined when the function is called.

My current attempt looks like this:

// Only change code below this line

function updateRecords(recordCollection, id, tracks, value) {

if(value === "") {

delete recordCollection[id][tracks];

} else if (tracks === "tracks") {

recordCollection[id][tracks] = records[id][prop] || [];

recordCollection[id][tracks].push(value);

} else {recordCollection[id][tracks].push(value);

return recordCollection;

}

console.log

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

I get an Unexpected token at (36,:55), i,e, the end of the last line, and when I try to run the code, nothing happens.

It really helps if you format your code.

Don’t rename this. That’s just confusing.

Where did you get this? You renamed the argument records, so this won’t work.

Why push to the tracks array when the property is not tracks?

This is not valid code.

You never close the braces for this else clause.

I have tried again with a more direct if/else if approach. At least it runs but still does not do what it is supposed to do for half the lines. Latest version:

// Only change code below this line
function updateRecords(records, id, prop, value) {
// if prop is not tracks and value is not "", leave alone 
if (prop !== "tracks" && value !== "") {
    records[id][prop] = value; 
// if not above, if prop is "tracks" and 
} else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
    records[id][prop] = [value];

} else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value);
// if value is empty string, delete the prop from object
} else if (value === "") {
    delete records[id][prop];
    
return records;
}

console.log(records, 5439,"artist", "ABBA");
}

I’m getting there, but … Thanks for all your help.
Did you mean indentation when you mentioned format?

Copying into here removes the indentation and colouring from the original code.

Odd that copy-paste is deleting the spaces before this }

In any case, look at your {}s. You only return inside of the last if-else clase. I don’t think that is where you meant to put that.

Formatting helps catch mistakes that like that:

// Only change code below this line
function updateRecords(records, id, prop, value) {
  // if prop is not tracks and value is not "", leave alone 
  if (prop !== "tracks" && value !== "") {
    records[id][prop] = value; 
  // if not above, if prop is "tracks" and 
  } else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
    records[id][prop] = [value];
  } else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value);
  // if value is empty string, delete the prop from object
  } else if (value === "") {
    delete records[id][prop];   
    return records;
  }

  console.log(records, 5439,"artist", "ABBA");
}

I finally worked it out. Many thanks for all your help, JeremyLT.