I can't seem to complete this challenge. Maybe it's because I'm doing something wrong and it keeps failing all the tests

Hello everyone,
I am asking for help to complete this challenge. For some reason, I just keep failing all the tests each time …so if you can tell me what I’m doing wrong then it would be much appreciated.

  **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].hasOwnProperty("tracks") !== true){
    recordCollection[id][prop] = [];
    recordCollection[id][prop].push(value);
  }
  else{
    recordCollection[id][prop] = value;
  }


return records;
}

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

console.log(updateRecords(recordCollection,  5439, "tracks", "Take a Chance on Me"));


  **Your browser information:**

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

Challenge: Record Collection

Link to the challenge:

My main comment is that you would find the task easier if you arranged your if else statements in the same order that the conditions are given:
1 If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
2 If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
3 If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.
4 If value is an empty string, delete the given prop property from the album.

What can I set my default statement to, at the end of else if chain …? Or is it not needed to have an else statement at the end of the else if chain…? I really don’t completely know that is why I’m asking this.

Solve the issues then return records. I’ve written some psuedo code below.

You return afterwards beacuse:
Your function must always return the entire record collection object.

if( problem1 ) { solution to problem 1 }
else if ( problem 2){ solution to problem 2}
else if( problem 3){ solution to problem 3}
else if(  problem 4){ solution to problem 4 }
return records;

Does this make sense?

1 Like

Yes, it does. Thank you, now I’ll try and do it the way you suggested.

One more thing. You can’t solve this unless you get your condions right. Your first one looks way off. it should be (and I haven’t tested this) more like:

// If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
if(prop !== "tracks" && value !== "")
1 Like

Okay, I’ll try . And what about the solution… is it correct…? Or should try and do it in a different way…?

  if(value !== "" && prop !== "tracks"){
    recordCollection[id][prop] = value;
  }

Remeber you can test by running your own console log at the bottom
eg:
set record 5439 title to “Bat out of Hell”

console.log(updateRecords(recordCollection,  5439, "albumTitle", "Bat out of Hell"));
2 Likes

I’ve fixed it the best I could but it’s still not passing any tests.

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

  if(prop !== "tracks" && value !== ""){
    recordCollection[id][prop] = value;
  }

  else if(prop === "tracks"  &&  recordCollection[id][prop] === undefined){
    recordCollection[id][prop] = [value];
  }

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

  return records;
}

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

console.log(updateRecords(recordCollection, 2548, "artist", ""));

Ok, I’ll have a look.

Before I do though You should note that you should be referencing records in your function as opposed to recordCollection That is what makes the function reuseable in different contexts

1 Like

Oh Okay, I’ll do it. So some of them are passing now… I guess that’s what I was missing but the ones where the ‘prop’ should be deleted when the value is not set, are not passing.

Make sure that you have them in the correct order.

I do, but in the console log, it is deleting the ‘prop’ if it doesn’t have a value in the sequence that I have.

ok, this works:

// Only change code below this line
function updateRecords(records, id, prop, value) {

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

gotta go have lunch hope you can work it out by comparing with your solution

1 Like

So I did shift the lines of code like you said and referenced the records correctly…I don’t know how I was this stupid…and it worked. So Thanks a lot for getting me through this challenge and hopefully I wasn’t too annoying.
Hope you have a great rest of your day :slightly_smiling_face:.