JS Record Collection Issue

Tell us what’s happening:
Hellow guys, I have been a lot of issues understanding the Basic JavaScript: Record Collection, I honeslty don’t quite understand there are too many things going here. any help to understand it would be fantastic
this is the link

Your code so far


// Setup
var collection = {
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(object, id, prop, value) {
if (object[id][prop] !== 'tracks' && object[id][value] !== ''){
  object[id][prop] = value;
} else if (object[id][prop] === 'tracks' && !object[id][prop].hasOwnProperty('tracks')){
  object[id][prop].push(value)
}else if(object[id][prop] == 'tracks' && object[id][value] !== ''){
 object[id][prop] = [value]; 
} else if ( object[id][prop] === 'tracks' && value !== '' ){
  object[id][prop] = [value]
} else if (value === ''){
  delete object[id][prop];
}

return object;
}

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


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0.

Challenge: Record Collection

Link to the challenge:

What do you want this comparison to be doing? I don’t think it does what you expect.

I was trying to do this
If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value.
and I wrote this

if (object[id][prop] !== 'tracks' && object[id][value] !== ''){
  object[id][prop] = value;

I suspected that was what you were trying to do.

That line checks if the id entry’s prop property has the value tracks and if the id entry’s value property is not the empty string. You instead need to check if the prop is set to 'tracks' and the value is not set to the empty string.

Do you mean something like this ?
if (prop !== ‘tracks’ && value!== ‘’)

Does the code work better when you try it?

It resolve most of the errors but I am still getting two errors ,

After updateRecords(collection, 5439, "tracks", "Take a Chance on Me") , tracks should have Take a Chance on Me as the last element

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

Awesome! Fewer errors is more better :slight_smile:
What does your current code look like?

Is definitely better than before thanks and this is how it looks like now

// Setup

var collection = {

  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(object, id, prop, value) {

  if (prop !== 'tracks' && value !== ''){

    object[id][prop] = value;

  } else if (object[id][prop] === 'tracks' && !object[id][prop].hasOwnProperty('tracks')){

    object[id][prop].push(value)

  }else if(object[id][prop] == 'tracks' && object[id][value] !== ''){

   object[id][prop] = [value]; 

  } else if ( object[id][prop] === 'tracks' && value !== '' ){

    object[id][prop] = [value]

  } else if (value === ''){

    delete object[id][prop];

  }

  return object;

}

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

You are close. I think your issue is here. This line checks if entry idpropertry prop has property 'tracks'. I think you mean to check if entry id has property 'tracks'.


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 (’).

Thank you for the “preformatted text” it helps a lot, I change the code you mentioned but nothing happens. below is what I have so far.

// Setup
var collection = {
  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(object, id, prop, value) {
  if (prop !== 'tracks' && value  !== ''){
    object[id][prop] = value;
  } else if (object[id][prop] === 'tracks' && !object[id].hasOwnProperty('tracks')){
    object[id][prop].push(value)
  }else if(object[id][prop] == 'tracks' && object[id][value] !== ''){
   object[id][prop] = [value]; 
  } else if ( object[id][prop] === 'tracks' && value !== '' ){
    object[id][prop] = [value]
  } else if (value === ''){
    delete object[id][prop];
  }

  return object;
}

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

Ok, bringing it all together, you should update these lines to match the logic in the first line

Also,

I don’t think you want to negate the results of hasOwnProperty

cool just change everything and now I am getting 5 error

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

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

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

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

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

and here is my code

// Setup
var collection = {
  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(object, id, prop, value) {
  if (prop !== 'tracks' && value  !== ''){
    object[id][prop] = value;
  } else if (pop === 'tracks' && id.hasOwnProperty('tracks')){
    object[id][prop].push(value)
  }else if(prop == 'tracks' && value !== ''){
   object[id][prop] = [value]; 
  } else if ( prop === 'tracks' && value !== '' ){
    object[id][prop] = [value]
  } else if (value === ''){
    delete object[id][prop];
  }

  return object;
}

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

Small typo

This piece was better when you were using object[id]. id is just a number. object[id] is the id entry.

After that, you will have one failing test.

I would fix that failing test by checking first if you are supposed to delete the property prop from entry id.

Cool we are almost there
just one error
After updateRecords(collection, 2548, "tracks", "") , tracks should not be set

// Setup
var collection = {
  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(object, id, prop, value) {
  if (prop !== 'tracks' && value  !== ''){
    object[id][prop] = value;
  } else if (prop === 'tracks' && object[id].hasOwnProperty('tracks')){
    object[id][prop].push(value)
  }else if(prop == 'tracks' && value !== ''){
   object[id][prop] = [value]; 
  } else if ( prop === 'tracks' && value !== '' ){
    object[id][prop] = [value]
  } else if (value === ''){
    delete object[id][prop];
  }

  return object;
}

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

That’s what I meant here. Your deletion check is after other checks and you are accidentally missing some cases. If you do that check first, then you know value !== '' in all other cases.

1 Like