Remove Elements from a Linked List by Index help

Tell us what’s happening:
I am not understanding where I am wrong in this code. it is showing that I am not able to return the element of the removed node. so please can anyone tell where I am going wrong?

Your code so far


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

var Node = function(element){ // {1}
  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++;
};

// Only change code below this line

this.removeAt = function(index){
if(index >= length || index < 0) return null
let current = head
let previous;
let currentIndex = 0

if(currentIndex === 0){
  head = null 
}else{
while(currentIndex !== index - 1 && current.next !== null){
  previous = current
  current = current.next
  currentndex++
}
previous.next = current.next

}
length--
return current.element
}
// Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Remove Elements from a Linked List by Index

Link to the challenge:

This line always returns null.
if(index >= length || index < 0) return null

Because length equals 0, it says …
if index is greater than or equal to 0
OR
if index is less than 0

that about covers all possible numbers

but how can be the length equals to 0 when we are incrementing length whenever we are adding node to the list.

this.removeAt = function(index){
console.log('hi',length)//logs hi 0
	if(index >= length || index < 0) return null

it is midnight here in Texas
I will be back in the morning

I solved the problem.The issue is not the length but the currentIndex which I am using in if condition

if(currentIndex === 0){
  head = null 
}

because I am writing it above the while condition, currentIndex is always 0 and it is always executing the if condition, not the else condition. so the code which passing all test cases is this:

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

  var Node = function(element){ // {1}
    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++;
  };

  // Only change code below this line

this.removeAt = function(index){
  console.log(length)
  if(index >= length || index < 0) return null
  let current = head
  let previous;
  let currentIndex = 0
  
    while(currentIndex !== index){
    previous = current
    current = current.next
    currentIndex++
    }

  if(currentIndex === 0){
    head = null 
    }else{
    previous.next = current.next
    }

length--
return current.element
}
  // Only change code above this line
}

and the length is showing correctly when I did the console.log. so check it out one more time.

good night and if you are seeing this in the morning i hope it will give some pleasure to you.