Object does not return added properties

Tell us what’s happening:
I added a property to an object and tested if this property is added inside of the code (right before i return the object). when i checked back the tests (test number 2) i found out that the property is not added for no reason (i have been staring for hours in it to check why it acted that way but to no avail)

Your code so far

// Setup
var collection = {
2548: {
  albumTitle: 'Slippery When Wet',
  artist: 'Bon Jovi',
  tracks: ['Let It Rock', 'You Give Love a Bad Name']
2468: {
  albumTitle: '1999',
  artist: 'Prince',
  tracks: ['1999', 'Little Red Corvette']
1245: {
  artist: 'Robert Palmer',
  tracks: []
5439: {
  albumTitle: 'ABBA Gold'

// Only change code below this line
function updateRecords(object, id, prop, value) {
  //print Input
  console.log("INPUT: ID: " + id + " & prop: " + prop + " & value: " + value);
  //check if the object is null
  if (object ==null){
      console.log("object is null");
      return false;

  //check if id is not a number
  if (id == null || isNaN(id)){
      console.log("Id is not a number");
      return false;
  //check if prop is not a string
  if (prop == null || prop == undefined || typeof prop != 'string'){
      console.log("prop is not a track");
      return false;

  //check if the value is not a string
  if (value == null || typeof value != 'string' || value === ""){
      console.log("value is not valid word");
      return false;

//for variable cases of prop (albumTitle, atrist, tracks)
    //albumTitle & artist are of type string 
    case "albumTitle":
    case "artist":
        object[id][prop] = value;
    //tracks are of type string[]
    case "tracks":
        //if <obj> does not have <prop> then add an array property with <prop>
        if (!object[id].hasOwnProperty(prop)){ //returns true in case 2
            object[id].prop = {value};
        } else {
        //add <value> to the last index in <prop>>
// console.log(object[id].prop) //this proves that the track has been added successfully 
return object;

//Test Passed
// console.log(1)
// var one = updateRecords(collection, 5439, "artist", "ABBA") // artist should be ABBA
// console.log(one[5439].artist);

//Test Passed
// console.log("1_1")
// var one_one = updateRecords(collection, 5439, 'artist', 'ABBA'); //artist should be ABBA
// console.log(one_one[5439].artist);

var two = updateRecords(collection, 5439, "tracks", "Take a Chance on Me") //tracks should have Take a Chance on Me as the last element.
var twoTrack = two[5439].tracks;
console.log(typeof twoTrack); //this should return array but instead it returns undefined
// console.log(twoTrack[twoTrack.length -1]);

// console.log(3)
// var three = updateRecords(collection, 2548, "artist", "") //artist should not be set
// console.log(three[2548].artist)

//Test Passed
// console.log(4)
// updateRecords(collection, 1245, "tracks", "Addicted to Love") //tracks should have Addicted to Love as the last element.

//Test Passed
// console.log(5)
// updateRecords(collection, 2468, "tracks", "Free") //tracks should have 1999 as the first element.

// console.log(6)
// updateRecords(collection, 2548, "tracks", "") //tracks should not be set

//Test Passed
// console.log(7)
// updateRecords(collection, 1245, "albumTitle", "Riptide") //albumTitle should be Riptide

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

I think your issue is here:

      if (!object[id].hasOwnProperty(prop)) {
        object[id].prop = { value };
      } else {

You are sending in the “tracks” as the prop variable but you are explicitly setting that property name to be called “prop”, not what is contained in the prop variable. So, we need to take care of that. The other problem is that we are setting that to be { value }, so that is an object literal using shorthand notation so it is an object with the property called “value” and that value as the value of the key/value pair.

That is why

console.log(typeof twoTrack);
// undefined

This is the same as:

console.log(typeof two[5439].tracks);
// undefined

But with your current code, if we did:

console.log(typeof two[5439].prop);
// object

because you are calling that property “prop”. It is an object, { value: "Take a Chance on Me" } for reasons already explained.

So, we need to fix that property name and we need to give it an array literal and not an object literal. When I do that, I get:

console.log(typeof two[5439].prop);
// object

It still says object because in JS arrays are objects and the typeof doesn’t differentiate. But I can check with:

// true

Or I can log out the actual array and see that it is an array.

1 Like

Thank you for the contribution; I am still confused because these tests contradicts what you said (IMHO).

    var two = updateRecords(collection, 5439, "tracks", "Take a Chance on Me") //tracks should have Take a Chance on Me as the last element.
    var twoTrack = two[5439].tracks;
    console.log(typeof twoTrack); //this should return array but instead it returns object
    console.log(two[5439].prop) //this should be an error because prop is undefined outside of the function domain
$ node test.js
INPUT: ID: 5439 & prop: tracks & value: Take a Chance on Me

Regarding the last point (Array.isArray); how come? i mean array is an object but object is not array (at least that is what i know) but why does it not return array instead of object?

that’s how typeof works, you would need to use isArray

no, with dot notation you are trying to access a property literally named prop, it is not a variable.

1 Like

Actually that is what I meant; I mean he explicitly said that .prop works but .tracks doesn’t and that contradicts the debugging. Thank you.