-3

Can anyone tell me why this code will not delete messages from the messages property of this object?

var facebookProfile = {
  name: "Bill Smith",
  friends: 0,
  messages: ["msg"],
  postMessage: function(message) {
    message = "hello";
    facebookProfile.messages.push(message);
  },
  deleteMessage: function(index) {
    index = 0;
    facebookProfile.messages.splice(index);
  },
  addFriend: function() {
    facebookProfile.friends = facebookProfile.friends + 1;
  },
  removeFriend: function() {
    facebookProfile.friends = facebookProfile.friends - 1;
  }
};
VLAZ
  • 18,437
  • 8
  • 35
  • 54
user16864
  • 1
  • 1
  • 1
    Why are you always setting `index = 0;`? What's the use of the parameter then? – ASDFGerte Jan 05 '20 at 19:18
  • I could really set it to anything. It's an assignment for a course where the only requirement is that deleteMessage does exactly that. When I try this code, I get the message that it does not delete messages, but it doesn't say why. – user16864 Jan 05 '20 at 19:21
  • users here are very quick to say it has been answered, but if you look at my code, and read the comments, I have already tried splice. – user16864 Jan 05 '20 at 19:24
  • Add a `console.log(facebookProfile.messages)` before and after calling `deleteMessage` and then post your console output. – SILENT Jan 05 '20 at 19:34
  • Thank you, Silent! I actually went ahead and removed index=0 and it worked :) – user16864 Jan 05 '20 at 19:37

1 Answers1

0
facebookProfile.messages.splice(index,1);

above code should work and fix your problem. In splice function, number of elements to be deleted is zero by default and hence you have to mention that too

Goku Africa
  • 155
  • 8