function deleteFriend(uid) {
    //Delete on user's side
    var userFriendArrBackup = friendArr;
    var friendFriendArrBackup = [];
    var verifyDeleteBool = true;
    var toDelete = -1;

    for (var i = 0; i < friendArr.length; i++){
      if(friendArr[i] == uid) {
        toDelete = i;
        break;
      }
    }

    if(toDelete != -1) {
      friendArr.splice(toDelete, 1);

      for (var i = 0; i < friendArr.length; i++) {
        if (friendArr[i] == uid) {
          verifyDeleteBool = false;
          break;
        }
      }
    } else {
      verifyDeleteBool = false;
    }

    if(verifyDeleteBool){
      removeFriendElement(uid);
      user.friends = friendArr;
      generateAddUserBtn(); //Regenerate the button for new friendArr

      firebase.database().ref("users/" + user.uid).update({
        friends: friendArr
      });

      //alert("Friend Successfully removed from your list!");
    } else {
      friendArr = user.friends;
      firebase.database().ref("users/" + user.uid).update({
        friends: userFriendArrBackup
      });
      alert("Delete failed, please try again later! (user)");
      return;
    }



    //Delete on friend's side
    verifyDeleteBool = true;
    toDelete = -1;
    var friendFriendArr;//Weird name, I know, but it's the friend's friend Array...

    for (var i = 0; i < userArr.length; i++){
      if(userArr[i].uid == uid) {
        friendFriendArr = userArr[i].friends;
        friendFriendArrBackup = friendFriendArr;
        break;
      }
    }
    for (var i = 0; i < friendFriendArr.length; i++){
      if (friendFriendArr[i] == user.uid){
        toDelete = i;
        break;
      }
    }

    if(toDelete != -1) {
      friendFriendArr.splice(toDelete, 1);

      for (var i = 0; i < friendFriendArr.length; i++) {
        if (friendFriendArr[i] == user.uid) {
          verifyDeleteBool = false;
          break;
        }
      }
    } else {
      verifyDeleteBool = false;
    }

    if(verifyDeleteBool){
      firebase.database().ref("users/" + uid).update({
        friends: friendFriendArr
      });

      //alert("Friend Successfully removed from their list!");
    } else {
      firebase.database().ref("users/" + uid).update({
        friends: friendFriendArrBackup
      });
      alert("Delete failed, please try again later! (friend)");
    }
  }
//This function deletes the friend from the user's data and updates the database with the updated list.
This function deletes the friend from the user's data and updates the database with the updated list.
This function deletes the friend from the user ' s data and updates the database with the updated list .
function deleteFriend(uid) {
    var userFriendArrBackup = friendArr;
    var friendFriendArrBackup = [];
    var verifyDeleteBool = true;
    var toDelete = -1;
    for (var i = 0; i < friendArr.length; i++){
      if(friendArr[i] == uid) {
        toDelete = i;
        break;
      }
    }
    if(toDelete != -1) {
      friendArr.splice(toDelete, 1);
      for (var i = 0; i < friendArr.length; i++) {
        if (friendArr[i] == uid) {
          verifyDeleteBool = false;
          break;
        }
      }
    } else {
      verifyDeleteBool = false;
    }
    if(verifyDeleteBool){
      removeFriendElement(uid);
      user.friends = friendArr;
      generateAddUserBtn(); 
      firebase.database().ref("users/" + user.uid).update({
        friends: friendArr
      });
    } else {
      friendArr = user.friends;
      firebase.database().ref("users/" + user.uid).update({
        friends: userFriendArrBackup
      });
      alert("Delete failed, please try again later! (user)");
      return;
    }
    verifyDeleteBool = true;
    toDelete = -1;
    var friendFriendArr;
    for (var i = 0; i < userArr.length; i++){
      if(userArr[i].uid == uid) {
        friendFriendArr = userArr[i].friends;
        friendFriendArrBackup = friendFriendArr;
        break;
      }
    }
    for (var i = 0; i < friendFriendArr.length; i++){
      if (friendFriendArr[i] == user.uid){
        toDelete = i;
        break;
      }
    }
    if(toDelete != -1) {
      friendFriendArr.splice(toDelete, 1);
      for (var i = 0; i < friendFriendArr.length; i++) {
        if (friendFriendArr[i] == user.uid) {
          verifyDeleteBool = false;
          break;
        }
      }
    } else {
      verifyDeleteBool = false;
    }
    if(verifyDeleteBool){
      firebase.database().ref("users/" + uid).update({
        friends: friendFriendArr
      });
    } else {
      firebase.database().ref("users/" + uid).update({
        friends: friendFriendArrBackup
      });
      alert("Delete failed, please try again later! (friend)");
    }
  }
function deleteFriend ( uid ) { var userFriendArrBackup = friendArr ; var friendFriendArrBackup = [ ] ; var verifyDeleteBool = true ; var toDelete = - 1 ; for ( var i = 0 ; i < friendArr . length ; i ++ ) { if ( friendArr [ i ] == uid ) { toDelete = i ; break ; } } if ( toDelete != - 1 ) { friendArr . splice ( toDelete , 1 ) ; for ( var i = 0 ; i < friendArr . length ; i ++ ) { if ( friendArr [ i ] == uid ) { verifyDeleteBool = false ; break ; } } } else { verifyDeleteBool = false ; } if ( verifyDeleteBool ) { removeFriendElement ( uid ) ; user . friends = friendArr ; generateAddUserBtn ( ) ; firebase . database ( ) . ref ( "users/" + user . uid ) . update ( { friends : friendArr } ) ; } else { friendArr = user . friends ; firebase . database ( ) . ref ( "users/" + user . uid ) . update ( { friends : userFriendArrBackup } ) ; alert ( "Delete failed, please try again later! (user)" ) ; return ; } verifyDeleteBool = true ; toDelete = - 1 ; var friendFriendArr ; for ( var i = 0 ; i < userArr . length ; i ++ ) { if ( userArr [ i ] . uid == uid ) { friendFriendArr = userArr [ i ] . friends ; friendFriendArrBackup = friendFriendArr ; break ; } } for ( var i = 0 ; i < friendFriendArr . length ; i ++ ) { if ( friendFriendArr [ i ] == user . uid ) { toDelete = i ; break ; } } if ( toDelete != - 1 ) { friendFriendArr . splice ( toDelete , 1 ) ; for ( var i = 0 ; i < friendFriendArr . length ; i ++ ) { if ( friendFriendArr [ i ] == user . uid ) { verifyDeleteBool = false ; break ; } } } else { verifyDeleteBool = false ; } if ( verifyDeleteBool ) { firebase . database ( ) . ref ( "users/" + uid ) . update ( { friends : friendFriendArr } ) ; } else { firebase . database ( ) . ref ( "users/" + uid ) . update ( { friends : friendFriendArrBackup } ) ; alert ( "Delete failed, please try again later! (friend)" ) ; } }
This function deletes the friend from the user's data and updates the database with the updated list.
This function deletes the friend from the user ' s data and updates the database with the updated list .
//Delete on user's side //Regenerate the button for new friendArr //alert("Friend Successfully removed from your list!"); //Delete on friend's side //Weird name, I know, but it's the friend's friend Array... //alert("Friend Successfully removed from their list!");