Basic JavaScript - Record Collection

I don’t understand why my code fails test case number 4. addicted to love is the only one that outputs and hence the last one so why does it fail?

// 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 == ‘artist’)
{
records[id][prop]= value;
}
if (prop == ‘albumTitle’)
{
records[id][prop] = value;
}
if (prop == ‘tracks’)
{
records[id][prop]= [value];

}
if (value == “”)
{
delete records[id][prop];
}
records = recordCollection;
console.log(recordCollection);
return records;
}

updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’);

// 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 == 'artist')
  { 
  records[id][prop]= value;
  }
  if (prop == 'albumTitle')
  {
    records[id][prop] = value;
  }
  if (prop == 'tracks')
  {
    records[id][prop]= [value];

  }
  if (value == "")
  {
    delete records[id][prop];
  }
  records = recordCollection;
  console.log(recordCollection);
  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/106.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

Don’t do this. Potentially you are overwriting your work here.

Are you handling all cases for tracks correctly?

Why these two hard coded prop values?

For point 1) you made me realize I didn’t define my variable correctly but now with “var records = recordCollections” I still cant get cased 3 solved. Also, I don’t think I’m overwriting my work because the function argument passes the variable “records” with the value “recordCollection” and in all my “if statements” I’m manipulating the “recordCollection”. so at the end I set my blank variable equal to the object I manipulated so I can pass it back to “updateRecords” 2) I believe I’m handling the case correctly because aconsole.log returned "“Addicted to Love” as the only value being stored in “tracks” for id 1245 yet it keeps saying that I’m failing it. 3) the prop value isn’t hard coded … it’s taking the value from the “prop function argument” and I have two if cases because, well actually I probably didn’t to repeat two if cases like that but that still doesn’t explain why my case 3 fails

You shouldn’t do that either. Why is that there?

That is not always true. You should not hard code global variables.

You don’t handle the case where the tracks array already exists.

No, but the value you are comparing again unnecessarily hard coded.

Because I like to put unnecessary code in my program. LOL. Ok I have it set to “Return recordCollections” instead of my madeup variable. But, I still have the same output and the same problem . Also, I made a comment mistake, Its only test case 4 and 7 Im only not passing , and the change didn’t influence anything.

You don’t handle the case where the tracks array already exists.

Yes I do, my program just overwrites the information

Don’t use the global variable! recordCollection is the global variable.

Ok… You don’t correctly handle the case where the tracks array already exists.

why can’t I use the global variable? Manipulating the global variable worked fine for all the other cases.

in test case 5 a tracks array already exist but I pass that one

Because 1) the instructions tell you to use records and 2) you will not always pass in the same object

You are deleting, not modifying on that test.

It would be easier if you post your lastest code.

The instructions say to use records, and I’m indeed using the records argument in all my statements. there’s nothing in the instructions that says I can’t play with the global variable.

my code is similar

// 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 == 'artist')
  { 
  records[id][prop]= value;
  }
  if (prop == 'albumTitle')
  {
    records[id][prop] = value;
  }
  if (prop == 'tracks')
  {
    records[id][prop]= [value];

  }
  if (value == "")
  {
    delete records[id][prop];
  }
  
  return recordCollection;
}

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

Dude. Don’t use global variables unless the instructions explicitly tell you to.

You still should ditch the hard coded values of ‘artist’ and ‘albumTitle’. This logic should happen whenever the prop is not tracks.

You are not handling the case where the array already exists. If the array already exists, the instructions tell you to add the value to the end of the array.

Do not use the global variable. Use the function argument. The tests don’t use the same input as you are using here.

Ok I finally passed, I definitely know that these test cases are bugged now, also my code is not the greatest. but I pass them all now. thanks for your help. I need to get some sleep now. Ill try to clean this up when I look at it again!

// 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 == 'artist')
  { 
  records[id][prop]= value;
  }
  if (prop == 'albumTitle')
  {
    records[id][prop] = value;
  }

 if (prop == 'tracks' && records[id].hasOwnProperty("tracks") == true)
  {
    records[id][prop].push(value);
    console.log(records[id][prop])

  }
    if (prop == 'tracks' && records[id].hasOwnProperty("tracks") == false)
  {
    records[id][prop]= [value];

  }
  if (value == "")
  {
    delete records[id][prop];
  }
  else
  {
    
  }
  return records;
}

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

The test cases are not bugged.