Javascript basic please help

// 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) {

  if(prop!="tracks" && value!=""){

    object[id][prop]=value;

  }

  else if(prop =="tracks" && !object[id].hasOwnProperty("tracks")) {

    object[id][prop]=value;

  }

  else if(prop=="tracks"&& value!=""){

    object[id][prop].push(value);

  }

  else if(value==""){

    delete object[id][prop];

  }

  return object;

}

updateRecords(collection, 5439, 'artist', 'ABBA');



//getting error: After


updateRecords(collection, 5439, "tracks", "Take a Chance on Me")

,

tracks

should have

Take a Chance on Me

as the last element.
why??

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hi,
I am new here as well. I have worked on this issue yesterday, scratching my head all evening. Then I have read the instructions again:

  • If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.

empty array is the key - console.log your collection and check whether this property is getting set as an array.

1 Like