Small mistake that is probably easy to solve but im dumb

I can’t figure out why but my .push() is incorrect. The rest of the code passed when I ran it but this won’t work. I ran it in vscode and it deletes all the tracks and replaces it with the track value that you call the function with.

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




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/83.0.4103.106 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

Add some console log for each condition and see what is happening. I guess you don’t want the solution.

I console logged the call for the function and it replaced the value of tracks with the value that was inside of the function call.

This is your problem. Your third block will never execute because “tracks” is not an empty string.

As an aside, you never need the value !== "" that you have because you are checking for that in the initial if. None of your elses will execute if value is an empty string.

1 Like
If prop isn't "tracks" and value isn't empty (""), update or set the value for that record album's property.

I don’t see a statement addressing this case in your code.

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

What is that going to to your tracks?

1 Like
function updateRecords(id, prop, value) {
  if (value == ""){
    delete collection[id][prop]
    }else if (prop == "tracks"){
    collection[id][prop] = collection[id][prop] || [] ;
    collection[id][prop].push(value);
  }else {
     collection[id][prop] = value;
  } 
return collection;
  }

So basically I have to check if property is ‘track’ first before checking if property is not an empty string because the function will count it as not an empty string first in the original code?

You can either check if prop is “tracks” first and then else the logic for the more general case or you could nest an if (prop === "tracks") in the block that you had before it. The key here is the else. An else condition will only be checked if the preceding conditions are false.

1 Like