Data Structures - Remove Elements from a Linked List

Tell us what’s happening:
Describe your issue in detail here.

Not able to pass the test, the test case is to decrease the length after removing the element, I do so and the size() method also giving the correct output.
Please let me know what I am doing wrong???

Your code so far

function LinkedList() {
  var length = 0;
  var head = null;

  var Node = function(element){
    this.element = element;
    this.next = null;
  };

  this.size = function(){
    return length;
  };

  this.head = function(){
    return head;
  };

  this.add = function(element){
    var node = new Node(element);
    if(head === null){
        head = node;
    } else {
      var currentNode = head;

      while(currentNode.next){
        currentNode  = currentNode.next;
      }

      currentNode.next = node;
    }

    length++;
  };

  this.remove = function(element){
    // Only change code below this line
    let current = head;
    if(element==head.element){
      head= current.next;
      length--;
    }
    let previous = current;
    while(current){
      if(element == current.element){
        previous.next = current.next;
        length--;
        return;
      }
      previous = current;
      current = current.next;
    }
    // Only change code above this line
  };
}

let ll = new LinkedList();
ll.add('a');
ll.add('b');
ll.add('c');
ll.add('d');
console.log('length before removing',ll.size());
ll.remove('d');
console.log('length after removing',ll.size());
console.log(ll.head());

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36

Challenge: Data Structures - Remove Elements from a Linked List

Link to the challenge:

What happen after this if execution ?

try modify your test case to remove the head element :grin:

let ll = new LinkedList();
ll.add('a');
ll.add('b');
console.log('length before removing',ll.size());
ll.remove('a');
console.log('length after removing',ll.size());

syntatic sugar problem… I’m too lazy to code now, need a break after LinkedList problem.