Create a Doubly Linked List

this challenge not working properly. Can someone test it?

can you provide a link to that challenge?

https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-doubly-linked-list/

Hmmm… I’m stumped. It worked for me.

Are you using Chrome browser? Did you change the name of any of the provided variables?

Maybe post your code and someone will see a bug.

var Node = function(data, prev) {
  this.data = data;
  this.prev = prev;
  this.next = null;
};
var DoublyLinkedList = function() {
  this.head = null;
  this.tail = null;

  this.add =function(element){
    // var newest = new Node(element,null)
    if(this.head){
      var temp = this.tail
      var newest = new Node(element,this.tail)
      newest.prev = this.tail;
      this.tail.next = newest;
      this.tail = newest;
      
      
    }else{
      this.head = new Node(element,null)
      this.tail = this.head;
    }
  }
  this.remove = function(element){
      if(this.head){
          if(this.head.data==element){
            this.head = this.head.next
          }

      if(this.tail){
        if(this.tail.data==element){
          this.tail = this.tail.prev
        }
      }
      var node = this.head;
      var prev=node.prev;
      var next = node.next

      while(node!=null){

        if(node.data==element){
          prev.next=next
          next.prev=prev;
        }

        prev=node;
        node=node.next
        next=node.next;
      }


    }else{
      return null;
    }
  }


  // change code below this line
  // change code above this line
};

var test = new DoublyLinkedList();
test.add('first')

test.add('second')

remove function refuses to work. Also i cant preview how code is working because JSON.stringify() wont convert this structure into a string, so can only guess where to seek the problem

You have minor bugs in the two edge cases removing head or removing tail

  1. The new head or new tail will need some housekeeping on next / prev
  2. After removing head or tail your while loop also executes

OK - I have taken a closer look at this challenge. (OK, really I took a nap and came back)

My “passing” solution does not satisfy this requirement below. So I think that there is a flaw in the testing software - that should have been caught.

while the remove method should remove all occurrences of a given element in the list.

So I guess the below should not be an issue but rather a requirement.

  1. After removing head or tail your while loop also executes