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; = null; 

  this.head = function(){
    return head;

  this.size = function(){
    return length;

        return true;
        return false;
  this.add = function(element){
    // Only change code below this line
    let newNode= new Node(element);
        let current=this.head;

    // 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:

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 is less than null here. No matter what the value of is, this while loop condition will always evaluate to false. Why? Because if is not empty, that means it is an object (Node) and objects are not less than null, so that would be false. Also, if 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?