Create a Linked List Class

Tell us what’s happening:
getting error : Cannot read property ‘element’ of null, need help

Your code so far


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.isempty=function(){
    if(this.head==null){
        return true;
    }else{
        return false;
    }
}
  this.add = function(element){
    // Only change code below this line
    let newNode= new Node(element);
    if(this.isempty){
        head=newNode;
        length++;
    }else{
        let current=this.head;
        while(current.next<null){
            current++;
        }current.next=newNode;
        length++;
    }

    // Only change code above this line
  };
}

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-linked-list-class

I am going to highlight the main problems with your code below.

You refer to this.head. Remember this.head is a function, so you need to call a function to get a return value.

Similar problem as the first I pointed out above. this.isempty is a function.

Same issue again.

Not sure exactly why you are checking if current.net is less than null here. No matter what the value of current.next is, this while loop condition will always evaluate to false. Why? Because if current.net is not empty, that means it is an object (Node) and objects are not less than null, so that would be false. Also, if current.next is null, null is not less than null, so that would be false.

The second issue above is I have no idea why you are trying to increment current with the ++ operator. If current was an object, after the above line current would be NaN.

All of the issues except the last two are probably just typos where you forgot to make the function calls. The last two are just bad logic. Can you explain what you were trying to acheive with the while loop, so maybe I can help guide you to the solution you were after?