Hello please I want the help I don't no why is wrong with this code

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

  **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(prop !== "tracks" && prop !== "" ) {
  records[id][prop] = value;
} else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false){  
  records[id][prop] = [value];
} else if (prop === "track" && prop === "") {
  records[id][prop].push(value);
} else if (value === "") {
  delete 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/102.0.5005.63 Safari/537.36 Edg/102.0.1245.33

Challenge: Record Collection

Link to the challenge:

Your logic flow for deleting a value doesn’t seem to be correct.

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

Which branch of your function gets run when you make this call?

the branch is : else if (value === “”) {
delete records[id][prop];

That’s not what I see…

function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && prop !== "") {
    console.log('over here!')
    records[id][prop] = value;
  } else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
    records[id][prop] = [value];
  } else if (prop === "track" && prop === "") {
    records[id][prop].push(value);
  } else if (value === "") {
    delete records[id][prop];
  }

  return records;
}

updateRecords(recordCollection, 2548, "artist", "");

This logs over here! so it’s going through the first branch, not the last.

It’s true that putting it first solves the problem but I would like to know if possible why it gives like this instead of like I put it first

I didn’t change your code except for putting in a console.log, so I’m not sure what you mean.

I solved the problem by putting if (value === “”) {
delete records[id][prop];
in first position

This is always going to be false
Prop is never “track” and is never “”, and can’t be two different values at the same time!

yes I changed the track to tracks and prop must respect the 2 conditions to be executed I think it is the role of &&.

A variable can’t have two different values at the same time, so the variable props is not able to be equal to "tracks" and to "" at the same time

alright i think i will keep that in mind for the next challenges

Well, you need to keep it in mind for this one if you want to try to solve it

I have already passed it but I have another question please I would like to know if I have to ask it in a new discussion space or I can do it here

If it’s for this challenge here, if it’s for a different challenge, use the ask for help button from that challenge

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