A problem with the Linked List

I do not understand the reason why this is not working.


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

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

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

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

this.add = function(element){
  let node = new Node (element);//Creación del nuevo nodo
  if(this.head===null){
      head = node
  }else{
    let currentNode = head;
    while(this.next){
        currentNode=this.next;
    }
    this.next=node;
  }
  length++  
};

}
  **Your browser information:**

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

Challenge: Create a Linked List Class

Link to the challenge:

You have a bit of a mess with your thiss. Sometimes you are using this when you don’t need it at all (like in this.head) in other places you are using this when you want to be referring to currentNode.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.