Basic JavaScript - Record Collection

Tell us what’s happening:

I’ve been struggling with this for a day or to. Having a hard time grasping the concept of code here. So far I’ve managed to get one out of seven right. I think that my code is totally wrong, can someone give me some tips without giving the answers straight out. Really want to solve this on my own.

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
let tracks = [];
function updateRecords(records, id, prop, value) {

if (value === "") {
  delete recordCollection[id][prop]
} else if (prop !== "tracks" && value !== "") {
  recordCollection[id][prop] = value;
} if (prop = "tracks" && value !== "") {
  recordCollection[id][tracks] = [];
}
recordCollection[id].tracks = (value.push);
  return records;
}

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

console.log(recordCollection);

Your browser information:

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

Challenge Information:

Basic JavaScript - Record Collection

One issue is that you should not use the global object

Note - you seem to have forgotten the else at the start of this?

Can you explain what this does? The value of the tracks property is supposed to be an array.

To be honest my I knew that my thought process was wrong here. The instruction was " First, if the album does not have a tracks property, assign it an empty array.", but not knowing how to do it i tried that knewing it was 99% the wrong answer.

I’ve changed the return from records; to return recordCollection in my function and got two out of seven right. Shouldn’t I have any more declaration of recordCollection since it’s already global? And can mess up the code

The assignment of an empty array part I like. That line is good.

But you are missing two pieces here - checking if you need to create an array, and your syntax’ for pushing to that array doesn’t work

Hello @ephraimtoll ;
Let this part of the code be part of the if else if chain. To make your code less redundant, your main if statement can be for checking if value is empty or not since you’ve repeated value!=="" multiple times in your solution

Ty! My new code is this :

let tracks = [];
function updateRecords(records, id, prop, value) {

if (value === "") {
  delete records[id][prop];
} else if (prop !== "tracks" && value !== "") {
  records[id][prop] = value;
}else if (prop == "tracks" && value !== "") {
  if (!recordCollection.hasOwnProperty("tracks")) {
    recordCollection[id].tracks = [];
  }
}
recordCollection[id].tracks = (value.push);
  return records;
}

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

Got 5 out of 7 right know since i made the changes. Any hint on where I might be wrong? Don’t want the answer hehe

Don’t use the global object ‘recordCollection’!

Careful. Do you absolutely always want to add the value to the tracks array?

And, double check how push works.

Why is this floating around in outer space? You should only put stuff inside of your function.

Thanks for taking your time and explaning in an educational way!

2 Likes

Ensure you are referencing the album instead of the collection (after making the changes suggested by @JeremyLT )

Re-evaluate the flow of your if-else-if and figure out where this needs to be

@ephraimtoll Did all the tests pass?

Not yet hehe. Still stuck. Managed to push the right values into to the array, but now I need to .unshift another album and not certain on how to do that. I’ve got 6 out of 7 right now. Just one to go. This is my code right now:

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

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

  

  return records;
}

updateRecords(recordCollection, 5439, ‘tracks’, ‘Take a Chance on Me’);

And the last thing I got to do is this :

After updateRecords(recordCollection, 2468, "tracks", "Free") , tracks should have the string 1999 as the first element.

I’ve tried to use .unshift to push it as the first element but as soon as I do that, the other “tracks” gets messed up.

Thanks for asking btw!

The push is better. It’s your check for the tracks array that’s a problem. You are adding tracks to the right place. But you aren’t adding tracks directly to the records object, so you shouldn’t be looking there to see if the tracks property exists.

I think I understand. I’m just wondering, since album 2468 wants me to add 1999 as the first element, how come push is better? From my understanding push is best when u want add something as the last element. Sorry if its a silly questions, just want to grasp the concepts and structures as good as possible.

Then add the value as the last item in the album’s tracks array.

You do want to add the new track to the end of the array

Hello, I’m also stuck here kindly help if you got the solution.

If you have a question about a specific challenge as it relates to your written code for that challenge need some help, click the Ask for Help button. This button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

You’ve got the solution if you klick on the get help button, but I suggest that you solve it on your own or maybe with som hints from the community, since if you want to become a developer or engineer you need to solve a lot of problems on your own. So it’s good to start to build that habit as early as possible. It’s perfectly fine to get stuck at a problem for days, eventually you will find the solution.

I also take help from chatgpt or google if I don’t understand any concept.

I’ve solved it hehe. After three friggin days. Thanks for all the tips! Really great tips without giving any answers away!

1 Like