Hello, I’m working on this challenge and for some reason my code for the **elementAt** method isn’t passing. These cases fail on FCC.

Blockquote

Your`elementAt`

method should return the element found at a given index in linked list.

Your`elementAt`

method should return`undefined`

if the given element is not found at a given index in linked list.

I ran several tests cases on my machine and it appears my method always returns the correct element for a given index and returns **undefined** for invalid ones. I looked at the solution code examples and they seem to be doing the same thing as my code except they use a **while loop** whereas I’m using a **for loop**. There must be some edge case where my code is failing but I can’t figure it out.

The **elementAt** method is at the bottom of this code block. Any ideas?

Thank you.

**Your code so far**

```
function LinkedList() {
var length = 0;
var head = null;
var Node = function(element){
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++;
};
this.remove = function(element){
var currentNode = head;
var previousNode;
if(currentNode.element === element){
head = currentNode.next;
} else {
while(currentNode.element !== element) {
previousNode = currentNode;
currentNode = currentNode.next;
}
previousNode.next = currentNode.next;
}
length --;
};
// Only change code below this line
this.isEmpty = function() {
return length === 0;
}
this.indexOf = function(element) {
let currentNode = head;
let index = 0;
while (currentNode.element !== element) {
if (!currentNode.next) {
return -1;
}
currentNode = currentNode.next;
index++;
}
return index;
}
this.elementAt = function(index) {
if (head === null) {
return undefined;
}
let currentNode = head;
for (i = 0; i < index; i++) {
if (currentNode.next === null) {
return undefined;
}
currentNode = currentNode.next;
}
return currentNode.element;
}
// Only change code above this line
}
```

```
**Your browser information:**
```

User Agent is: `Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0`

**Challenge:** Search within a Linked List

**Link to the challenge:**