Search Within a linked list: elementAt method not passing

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:

If you put a console.log('hello'); in the loop, it doesn’t ever print, which means the loop doesn’t ever run. So what’s wrong with the loop here? Fixing this does pass the test.

Thanks for the reply @jeremy.a.gray. I do see the for loop never logs “Hello” when I run it in the FreeCodeCamp editor. However, when I run it on my own computer, it works just fine. Strange…

I ended up editing my code to use a while loop instead and now it passes.

  this.elementAt = function(index) {
    if (head === null) {
      return undefined;
    }
    let currentNode = head;
    let currentIndex = 0;

    while (currentIndex !== index) {
      if (currentNode.next === null) {
        return undefined;
      }
      currentNode = currentNode.next;
      currentIndex++;
    }
    return currentNode.element;
  }

I still don’t quite understand why the while loop works but the for loop doesn’t. They both seem functionally the same in this case. I’m not sure if there’s something weird about the FCC tests or if I’m just being stupid. (Probably the latter) :sweat_smile:

It didn’t on mine. Probably a browser difference. It did print when I ran it with node locally, even though I was hoping it would barf loudly. I double checked the console in chrome and it does complain and flag the mistake. Putting a 'use strict'; at the top forces node to complain too. So depending on your brower settings, you may want to debug these challenges with the browser console open and it’s javascript engine set to be as pedantic as possible.

The for works:

  for (i = 0; i < index; i++) {
//     ^ this variable is not initialized
  for (let i = 0; i < index; i++) {
//     ^ this variable is initialized

The while works because there is no missing initialization.

Oh, wow! I completely missed the fact that I didn’t initialize the variable in the for loop. It’s always the dumbest things that trip me up! :rofl: I’ll definitely make sure to include 'use strict' in the future when doing these problems in node.

Thank you, @jeremy.a.gray!