Not sure what I'm doing wrong. Can't satisfy one of the conditions

Tell us what’s happening:
I’ve seen some of the solutions, but am trying to do it my way and can’t seem to understand why my code isn’t working.
Missing one of the conditions.

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"
}
};

// 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].hasOwnProperty(prop) == false) {
  collection[id][prop] = [value];
}
else if (prop == "tracks" && collection[id].hasOwnProperty(prop) == true) {
  collection[id][prop].push(value);
}
else if (value === "") {
  delete collection[id][prop];
}
else {
     collection[id][prop] = value;
}
return collection;
}

updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

what error are you getting?

You are trying to delete tracks in an else-if, but since the else-if above that one is true (where you push), the line with the deletion is never reached. As soon as one if-statement in this chain is true, all the rest gets ignored …

1 Like

// running tests

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

// tests completed

one of the two statements above where prop == "tracks" is the first to be true, so the value === "" condition is never reached

how to make so that instead it’s reached?

1 Like

just changed the order and it worked.
It reads from the top down.

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

before


else if (prop == "tracks" && collection[id].hasOwnProperty(prop) == false) {
    collection[id][prop] = [value];
  }
  else if (prop == "tracks" && collection[id].hasOwnProperty(prop) == true) {
    collection[id][prop].push(value);
  }
2 Likes

Some of the answers look much simpler than my multiple else if's.
How could I simplify it and use less code?
Should I use only if's?
What I mean is there a better way to look at this problem and resolve it in a more succinct way? Still working on thinking like a developer.

try looking at an algorithm like this with multiple possibilities like a decision tree

is there a way to make it shortest and make each condition repeat only once?

example

value === "" ?  -- true -> then delete
   |
false
   |
prop === "tracks" ?

try to complete the tree with all the conditions

2 Likes
// value === "" ?  -- true -> then delete
if (value === "")  {
    delete collection[id][prop];
  }
// prop === "tracks" ?
else if  (prop === "tracks" ) {
// create empty tracks array if required
 collection[id][prop] = collection[id][prop] || []; 
 collection[id][prop].push(value);
}
// everything else
else {
collection[id][prop] = value;
}

P.s Had to look this one up on stack overflow

collection[id][prop] = collection[id][prop] || [];

I had no idea it meant (create empty tracks array if required)
Still don’t really understand the line.

try looking at this article:

1 Like

thank you @ilenia.
I had missed the short circuit property and that’s a great thing to know.
So I understood the second part of it.
What is this doing exactly?

collection[id][prop] = collection[id][prop]

comparing with itself? is it a boolean expression?
Not sure how it’s checking if there is a value to prop already in the object.

= is assignment operator
the value resulting from the logical expression is assigned to collection[id][prop]

Thank you so much @ilenia.
It was right in front of my face :roll_eyes: and I didn’t see it.