About Record Collection

hi;
this challenge seemed very confusing for me as a beginner. So I’m sorry if it’s a too simple question.
In this solution code about the second if statement we say that:
if the prop is “tracks” and the album doesn’t have the tracks property, add tracks property and give the value inside it.
But I think this if statement also should have && value != "" because this is also a possibility, isn’t it?

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 (prop != "tracks" && value != "") {
  object[id][prop] = value;
}
if (prop == "tracks" && !object[id].hasOwnProperty("tracks")) {
  object[id][prop] = value;
}
if (prop == "tracks" && value != "") {
  object[id][prop].push(value);
}
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

1 Like

Checking for valid properties is a good idea in general, so you’re right about that. If we’re talking about improving the code, I would also say that it doesn’t make sense for us to check value ==="" three times. You could instead do something like

if (!value) delete
else if (prop === "tracks")
   if...
   else...
else object[id][prop] = value
2 Likes

I really like this simplification of code :smiley:
So now I’ve learned that “!” before a property means that property doesn’t have a value. is this true?

to be precise, the tracks property doesn’t exist when that is true

if value === "" is true then the property is to be deleted

the challenge is always giving a valid input

in case in which the tracks property doesn’t exist and there is "tracks", "" this code wouldn’t work

in a real life situation you would need to consider also this, for this challenge which has perfectly logical input and so it’s totally unrealistic, this works

1 Like

no, it just converts value to a Boolean value, flipping it
so there are two situations
!"" makes true, meaning that first statement is executed

and !"anything else" that makes false, meaning it’s not executed

As @ieahleen says, I’m just making use of the fact that an empty string is falsy. ! is the “not” operator so it makes falsy values true. if (!thing) is “if not-thing”, or “if thing is falsy”. If we want to more precisely check for empty strings, you could use if(value === "") instead.

@ieahleen thank you for the answers. I couldn’t see the fact that if the value is empty the property will be deleted. So you are right for this specific challenge that wouldn’t me necessary.
and @ArielLeslie I understood that the pc interprets !value code as it is empty string. But about your code, I couldn’t use another if statement into an if statement. I mean I used as you did here:

if (!value) delete
else if (prop === "tracks")
   if...
   else...
else object[id][prop] = value

but in the third else, it gave an error. I’m really a beginner so maybe I made a mistake so I let it go for now. Maybe I’ll understand the reason when I have more experience.

Thank you again. :+1:

What’s your current code? We can help you debug it.

I’ve solved the “else” problem but I have this problem right now.
This challenge made me understand why people give up coding :woman_facepalming:
if you help me, this will be great.
my code is here:

function updateRecords(object, id, prop, value) {
  if (value === ""){
    delete object[id][prop];
  }
  else if (prop == "tracks") {
    if (!object[id].hasOwnProperty) {
      object[id][prop] = value;
    }
    else 
    {
      object[id][prop].push(value);
    }
  }
  else {
    object[id][prop] = value;
  }
  
  return object;
}

and the error is this:

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

I think it’s about the “push” but I think I used it right :confused:

You forgot to make “tracks” be an array.

@ArielLeslie really sorry for taking your time for such a silly mistake :rofl:
Thank you so much

Not at all! That’s what I’m here for.

If we want to make your code look a little more slick, we actually don’t need an if/else inside that first else if.
We could do:

else if (prop == "tracks") {
    if (!object[id].hasOwnProperty) {
      object[id][prop] = [];
    }
    object[id][prop].push(value);   
  }

By the way, it’s conventional to either dangle {}s on their own line (personally I think that style wastes space) or keep then with the associated control statements. You’re mixing the two styles.