.push replaces, and does not append to array in object

Tell us what’s happening:
I just need to satisfy the last part of this challenge:

After updateRecords(2468, "tracks", "Free") , tracks should have "1999" as the first element.

It appears that when my code runs, the .push method doesn’t push the value onto the array, but rather replaces the string all together. I tested and checked the output in my browser console to confirm this. Can anyone tell me why?

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

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

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

return collection;
}

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

CONSOLE OUTPUT:
updateRecords(2468, “tracks”, “Free”);
{…}
1245: Object { artist: “Robert Palmer”, tracks: }
2468: {…}
album: “1999”
artist: “Prince”
tracks: Array [ “Free” ]
: Object { … }
2548: Object { album: “Slippery When Wet”, artist: “Bon Jovi”, tracks: (2) […] }
5439: Object { album: “ABBA Gold” }
: {…

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0.

Challenge: Record Collection

Link to the challenge:

I don’t know if this is the issue but typeof is not a function. Check out how it’s used on MDN.

1 Like

That is interesting, as I saw it used as a function elsewhere. It’s more interesting to say that just because of that change, it did pass, but now a separate point isn’t passing now.

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

What does your latest code look like?

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

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

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

  return collection;
}

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

In the console I am getting a type error saying that the collection[id][prop] is undefined - however I am looking for that and trying to set it to the value as it looks like I am trying to with the function. I can do this manually in the console but it doesn’t work in the function for some reason.

console output:
typeof collection[5439][“album”];
“string”
typeof collection[5439][“tracks”];
“undefined”
collection[5439][“tracks”] = [“Take a chance on me”];
Array [ “Take a chance on me” ]

typeof returns a string value, so the following if statement would evaluate to false if typeof collection[id][prop] is undefined. Also, not sure why you are even using typeof here. If you what to check if collection[id][prop] is undefined, you can do it without the typeof.

1 Like

That’s it, just typeof isn’t needed. Thanks!