Record Collection - help with the deleting the prop

Record Collection - help with the deleting the prop
0.0 0

#1

Tell us what’s happening:
I’ve passed all tests but the deleting doesn’t work. Help please.

Your code so far


// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line

function updateRecords(id, prop, value) {
  if(prop !== "tracks" && value !== null){
    collection[id][prop] = value;

  } else if(prop === "tracks" && collection[id][prop] == null){
    collection[id][prop] = [];
    collection[id][prop].push(value);

  } else if(prop === "tracks" && value !== null){
    collection[id][prop].push(value);

  } else if(value === null) {
    delete collection[id][prop];
  }
  
  return collection;
}

// Alter values below to test your code
updateRecords(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/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection


#2

The delete keyword deletes properties, not values.

The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.

Mdn https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete


#3

Oh! well in that case what do I do with the last requirement?


#4

Right, but I still think the delete operator is what you want. It’s in the instructions.

The first problem I see is:

  if(prop !== "tracks" && value !== null){

and the instructions:

If prop is “tracks” and value isn’t empty ("") …

You are assuming that null and an empty string are the same thing - they are not.

The other problem I see is

  } else if(prop === "tracks" && value !== null){
    collection[id][prop].push(value)

This does not account for the possibility that the property tracks does not exist yet. How do you push onto something that doesn’t exist?

Also, for the sake of readability, I’d do:

  } else if(prop === "tracks" && value !== null){
    collection[id].tracks.push(value)

… you’ve already established that it is tracks.


#5

I’m sorry I posted one before making the corrections to the else if statement. I did add the empty array and pushed the value in.
If null and empty string are not the same then how do I represent it in the code? Cause I passed the tests relating to that, it’s the delete tests that I haven’t been able to pass.
I am at a loss for the deletion part. If I have to use delete and not the way I used then how do I do it then?


#6

You have to test for null using the or operator ||, and the empty string ''

Mdn https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators


#7

Does it ask you to check for null?

As JM says, you can do

(prop === "tracks" && (value !== null || value !== ""))

or I don’t see where it says anything about null so you could do:

(prop === "tracks" && value !== "")

or if you really want, because they are both falsy, you could do:

(prop === "tracks" && value)

#8

Thanks you guys. I’m posting the code that worked feel free to comment on it.

// Setup
var collection = {
“2548”: {
“album”: “Slippery When Wet”,
“artist”: “Bon Jovi”,
“tracks”: [
“Let It Rock”,
“You Give Love a Bad Name”
]
},
“2468”: {
“album”: “1999”,
“artist”: “Prince”,
“tracks”: [
“1999”,
“Little Red Corvette”
]
},
“1245”: {
“artist”: “Robert Palmer”,
“tracks”: [ ]
},
“5439”: {
“album”: “ABBA Gold”
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
if(prop !== “tracks” && value !== “”){
collection[id][prop] = value;

} else if(prop === “tracks” && collection[id][prop] == undefined){
collection[id][prop] = [];
collection[id][prop].push(value);

} else if(prop === “tracks” && value !== “”){
collection[id][prop].push(value);

} else if(value === “”) {
delete collection[id][prop];
}

return collection;
}

// Alter values below to test your code
updateRecords(5439, “artist”, “ABBA”);