Can't push value to an empty array

Tell us what’s happening:
So far all of the tests passed except when i try to push an element in an already created empty array. Not sure exactly, but I think I might have created an array in an array with my code ( so like this []) and then pushed a value in this array ( [,value] ), but not sure how I should fix this problem.

  **Your code so far**

// Setup
const recordCollection = {
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(records, id, prop, value) {
if (value == "") {
  delete records[id][prop];
}
else {
  if (prop == "artist") {
    records[id]["artist"] = value;
  }
  else {
    if (records[id].hasOwnProperty(prop)) {
      records[id]["tracks"].push(value);
    }
    else {
      if (records[id]["tracks"] == []) {
        records[id]["tracks"].push(value);
      }
      else {
        records[id]["tracks"] = [];
        records[id]["tracks"].push(value);
      }
    }
  }
}
return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0

Challenge: Record Collection

Link to the challenge:

I would check this. Test if it works like you think it does (it doesn’t!).

2 Likes

Hm since that case doesn’t work, I tried to make it work with length() but then apparently it won’t check anymore if the object with the given ID has any array in it. So how would I possibly check if an array is empty?

You aren’t checking if the array is empty though. You need to know if the array exists. If the tracks were deleted, then that property no longer exists.

Still didn’t get it quiet to work sadly. Do you have any tips maybe? Maybe an if case i’m completely missing or a function? I’ve already used all the tips that have been given to me, like using delete or hasOwnProperty, still I really don’t know what I’m missing here…

function updateRecords(records, id, prop, value) {
  // Should use === instead of ==
  if (value == "") {
    delete records[id][prop];
  } else {
    // Why are you treating 'artist' as a special case?
    // What about other properties that are not 'tracks'?
    if (prop == "artist") { // Also, === is better than ==
      records[id]["artist"] = value;
    } else {
      if (records[id].hasOwnProperty(prop)) {
        records[id]["tracks"].push(value);
      } else {
        // This will never be true and should not be here
        if (records[id]["tracks"] == []) {
          records[id]["tracks"].push(value);
        } else {
          // This will always execute instead of the if block
          records[id]["tracks"] = [];
          records[id]["tracks"].push(value);
        }
      }
    }
  }
  return records;
}

You might want to google something like “javascript check if a value is an array”