Unable to understand what is missing in my solution

I have re-edit my post with comments.

object[id].hasOwnProperty(prop)

object[id][prop] == value

I am not hard coding because the function will lose its meaning of having arguments. so e.g if params for checking value is empty equals to collection[2548]["tracks"] == ''

how is this checking what the value of prop is?

Did you see the 4 required cases? You seem to be attempting to follow a different set of instructions.

1 Like

Rather than simply converting the conditions as written, you have altered what the conditions are to match what you think they are.

So

prop isn’t “tracks” and value isn’t an empty string

Is not remotely the same as

if object with given id does not have the given property AND object with given id’s prop is not equal to value

You are not reading what the instructions say here, and instead you’re trying to attach your own interpretation. The conditions are much simpler than what you’re trying to do, they are almost exactly the same as the version written in English in the instructions.

So to try to explain without just telling you the answer:

function example (a) {
  // Here
}

In this function, where I have written // Here, how do I check what the value of a is? Not something you think a should be, I mean the value of a

1 Like

is that right? without an if condition I would just return value of a

That’s how you return the value of a. How do you check if a is 5?

using if conditon i.e.if(a == 5)

Yep. How do you check if a is the string "tracks"?

same as above but changing the right side to string i.e. if(a == "tracks")

Yep. And how do you check if a is an empty string?


prop and value are just variables.

How do you

  • check if prop is not “tracks” and value is not an empty string

  • check if prop is “tracks” and value is not an empty string

  • check if value is an empty string

  • check if prop is not “tracks” and value is not an empty string
    if(prop !== ‘tracks’ && value !== ‘’)
  • check if prop is “tracks” and value is not an empty string
    if(prop == ‘tracks’ && value !== ‘’)
  • check if value is an empty string
    if(value == ‘’)

is the correct?

1 Like

Now we’re cooking with gasoline! That’s 3 of the 4 cases right there.

  • If prop is “tracks” but the album doesn’t have a “tracks” property

This is the only one left. This is the only case that should reference object[id].

2 Likes

so the above 3 cases are correct?

Do u mean it should be
if(prop == 'tracks' && object[id].hasOwnProperty('tracks')
is it correct now?

2 Likes

There you go. You have all of the cases. Now you need to fill in the correct actions

function updateRecords (object, id, prop, value) {
  if (/* If prop isn’t “tracks” and value isn’t an empty string */) {
    //  update or set that album’s prop to value
  } else if (/* If prop is “tracks” but the album doesn’t have a “tracks” property */) {
    // create an empty array and add value to it
  } else if (/* If prop is “tracks” and value isn’t an empty string */) {
    // add value to the end of the album’s existing “tracks” array
  } else if (/* If value is an empty string */) {
    // delete the given prop property from the album
  }
  // Your function must always return the entire object.
  return object;
}

how do we know if that is the only case that needs [id], why not use it in 3 other cases?

This right here is the only condition that talks about a specific album. The other conditions only talk about the prop or value.

now here is my confusion, we have a nested object i.e.

var collection = { 2548: { albumTitle: 'Slippery When Wet', artist: 'Bon Jovi', tracks: ['Let It Rock', 'You Give Love a Bad Name'] },
so if we want to check for a prop for each album, we need to give its i.d. for example, the first statement ask

** If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .*


so to find prop isn't tracks, we need id because the prop are nested inside id's. isn't it? if(object[id].hasOwnProperty('tracks') && object[id][prop]

Condition:

If prop isn’t tracks and value isn’t an empty string

Outcome:

update or set that album’s prop to value


The condition only talks about prop and value. Remember, prop and value are just variables. You just showed me how to check if a variable meets a condition.

The outcome says what should happen to the specific album in the array if the condition is met. When you get to the outcome, then you need to modify object[id][prop] in the correct way.


If this helps

  • IF prop isn’t tracks and value isn’t an empty string, THEN update or set that album’s prop to value .
  • IF prop is tracks but the album doesn’t have a tracks property, THEN create an empty array [for the tracks of that album] and add value to it.
  • IF prop is tracks and value isn’t an empty string, THEN add value to the end of the album’s existing tracks array.
  • IF value is an empty string, THEN delete the given prop property from the album.

The IF part goes in the conditional of the if-else statements. The THEN goes in the body of each case.

I didn’t understand what was my mistake when some other folks pointed out I m making it too complicated etc.