Delete a Leaf Node in a Binary Search Tree - last test failure

Tell us what’s happening:

I am having trouble with the last test case. I have tested my code by adding to the binary tree. And then, I removed the new node with .remove function. It removes the leaf node like I expected. I do not know what is wrong with my code.

Thanks.

Your code so far


var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}

function BinarySearchTree() {
this.root = null;
this.add = function(element) {
  let current = this.root;
  if (!current) {
    this.root = new Node(element);
    return;
  } else {
    const searchTree = function(current) {
      if (current.value > element) {
        if (current.left) {
          return searchTree(current.left);
        } else {
          current.left = new Node(element);
          return;
        }
      } else if (current.value < element) {
        if (current.right) {
          return searchTree(current.right);
        } else {
          current.right = new Node(element);
          return;
        }
      } else {
        return null;
      }
    };
    return searchTree(current);
  }
};


// case 1: target has no children, change code below this line
this.remove = function(item) {
  let current = this.root;
  let parent = current;
  while (current) {
    if (item == current.value) {
      if (this.root.value === current.value) {
        this.root = null;
      }
      else if (parent.left.value === item) {
        parent.left = null;
      }
      else {
        parent.right = null;
      }
      break;
    }
    parent = current;
    if (item < current.value) {
      current = current.left; 
    }
    else {
      current = current.right;
    }
  }
}
}

let x = new BinarySearchTree();
x.add(2);
x.add(1);
x.add(3);
displayTree(x);
x.remove(3);
displayTree(x);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Delete a Leaf Node in a Binary Search Tree

Link to the challenge: