Unable to move forward on Record collection

Tell us what’s happening:
I’m getting undefined for this code output. I’ve checked out a couple of help posts but nothing has helped so far. I’m guessing “value” is undefined. I’m not able to understand why its undefined

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

console.log(updateRecords(1245, "tracks", "Addicted to Love"));

Your browser information:

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

Challenge: Record Collection

Link to the challenge:

Without reading all your code…

Hint 1:

if (prop === "tracks" && collection[id].hasOwnProperty("prop") == false)

There’s a issue with this line.

I’ve removed the quotes, same output.
“Cannot read property ‘push’ of undefined”

I’ve edited that part to:-
What I mean to do:- if the collection.if does not have tracks, create an array collection[id][prop] and then assign value to that array.

if (prop === “tracks” && collection[id].hasOwnProperty(prop) == false)
{
collection[id][prop] = ;
collection[id][prop] = value;

}

Think about that error.

“Cannot read property ‘push’ of undefined”.

What does that mean?

.push is a method on arrays.

Example:
let x = [1, 2, 3];
x is an array.
x.push(4) would work.

let z = undefined;
z is not an array.
so z.push will give you that error.

Think of these problems as exposure. The goal is not to solve them. The goal should be to get exposed to different types of errors and understand why they happen and how to fix them. I read nothing further of your code but understood what that error meant. Try to do that.

Thank you!! Yes Im trying to do push within an object. :slight_smile: okay let me go figure this out.

Always use === and never ==

People will try to convince you otherwise. Ignore their advice.

Example,
Enter your years of experience: 0

If you checked their answer using == '' hoping to prevent an empty answer, you’d have a bug in your code.

However, if you used === '' there wouldn’t be a bug, because triple equals checks for same type and value. Whereas double equal only checks for value.

Thanks! I’ll keep that in mind.

Push only works on arrays, not objects.

I got through this!! Much obliged!! :smile:

1 Like