Freecodecamp: Record Collection incorrect solution passed all tests

Created on 5 Jun 2016  路  8Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge Record Collection has an issue.
User Agent is: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:


// 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"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line

function updateRecords(id, prop, value) {
  if (value !== "" && prop !== "tracks") {
    collection[id][prop] = value;
  } else if (prop === "tracks" && value !== "") {
    /*if(collection[id].hasOwnProperty(prop))
      collection[id][prop].push(value);
    else
      collection[id][prop]=[value];
    //collection[id][prop].push(value);*/
    collection[id][prop]=[value];
  } else if (value === "") {
   delete collection[id][prop];
  }

  return collection;
}



// Alter values below to test your code
updateRecords(2548, "tracks", "ABBA");


This shouldn't work because its redefining tracks instead of pushing value to it (in the case when value isn't null).But it passed all tests .So ,you should not only just check that the last element of tracks should be val but also that the previous array elements are also present in it or maybe check that the size of tracks is increased and isn't reduced/unaltered (if tracks property is present).

help wanted

Most helpful comment

@eljaygalvez you got that error because in the collection object the object with id 5439 doesn't have tracks property predefined .So when you try to push into the tracks property (which is undefined currently for this object) ,the push operation cannot be performed as it is only defined for arrays.If you will see the commented part in my solution ,you will be able to find the correct solution

All 8 comments

Hi,
I also got stuck in this challenge.

Here's my code:
`// 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"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {

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

return collection;
}

// Alter values below to test your code
updateRecords(5439, "tracks", "Take a Chance on Me");
`

I still don't understand why it is showing me this,

TypeError: Cannot read property 'push' of undefined.

Shouldn't we use .push to add the value onto the end of the array. It got me confused.
But for now, I'll copy your code so I could move on. haha.

@eljaygalvez you got that error because in the collection object the object with id 5439 doesn't have tracks property predefined .So when you try to push into the tracks property (which is undefined currently for this object) ,the push operation cannot be performed as it is only defined for arrays.If you will see the commented part in my solution ,you will be able to find the correct solution

@UtkarshShukla7 Thanks man for the explanation. This was so frustrating, I think every other solution using google search is not correct for this challenge update. As you said, it's "re-defining" track instead of pushing it?

@UtkarshShukla7 Well spotted. This is truly an issue in the code checking routine.

@UtkarshShukla7

So ,you should not only just check that the last element of tracks should be val but also that the previous array elements are also present in it or maybe check that the size of tracks is increased and isn't reduced/unaltered (if tracks property is present).

Great point. There should be some additional tests that check for the pre-existing elements in the arrays after new ones have been pushed to it.

@erictleung Thanks for the review ! Also ,one test can be added to check if tracks property is an array before pushing into it but I guess we can let it go as we know how our object looks like and the description states that you have to push value into it (which tells that tracks would always be an array, if present).

@UtkarshShukla7 the pushing to an non-existent array issue should be resolved soon with #9034 once it's pushed to the live site.

@erictleung That seems to be covered then . :+1:

Was this page helpful?
0 / 5 - 0 ratings