Record Collection TESTS problem

Hello, I can’t understand why my code doesn’t pass 4 out off 7 tests. I read it many times and can’t see the problem.

Tests:

PASSED
After updateRecords(recordCollection, 5439, "artist", "ABBA") , artist should be the string ABBA

NOT
After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me") , tracks should have the string Take a Chance on Me as the last element.

PASSED
After updateRecords(recordCollection, 2548, "artist", "") , artist should not be set

NOT
After updateRecords(recordCollection, 1245, "tracks", "Addicted to Love") , tracks should have the string Addicted to Love as the last element.

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

NOT
After updateRecords(recordCollection, 2548, "tracks", "") , tracks should not be set

PASSED
After updateRecords(recordCollection, 1245, "albumTitle", "Riptide") , albumTitle should be the string Riptide

My code:

// 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' && value !== ''){
records[id][prop] = value;
}
else if (prop === 'tracks' && records.hasOwnProperty(tracks) === false){
records[id][prop] = [value];
}
else if (prop === 'tracks' && value === ''){
record[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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15

Challenge: Record Collection

Link to the challenge:

There is a small logic error in here. How exactly does this if(...) translate? What do you mean for it to say, and what does it actually say?

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hi,
what I mean is:
if prop is tracks and value isn’t an empty string then add this value to the end of prop.

You are right us not correct so I corrected:

else if (prop === ‘tracks’ && value !== ‘’)
{
record[id][prop].push(value);
}

but it didn’t change anything, still same tests are passing and same are not passing.

It helps to post your full updated code after each change

function updateRecords(records, id, prop, value) {
  if (prop !== 'tracks' && value !== '') {
    records[id][prop] = value;
  } else if (prop === 'tracks' && records.hasOwnProperty(tracks) === false) {
    records[id][prop] = [value];
  } else if (prop === 'tracks' && value !== '') {
    record[id][prop].push(value);
  } else if (value === '') {
    delete records[id][prop];
  }

  return records;
}

You made a fix, but there is still another bug in your code.

  } else if (prop === 'tracks' && records.hasOwnProperty(tracks) === false) {

Can you explain this line to us? I think explaining may help you see the bug in this line.

1 Like

I mean hère:
if prop is tracks but there is nothing assigned to it, then add value to prop’s array.

thanks for your help!

Ok, but does this check what you want? Does it check if `there is nothing assigned to it’? What it do you want to check? There are two bugs with this chuck of code.