Implement Linked List Operations - Implement Linked List Operations

Tell us what’s happening:

getAt function quiz still failing thought i tested it and it is working, can someone tell me what i do wrong?

Your code so far

function initList() {
  return {
    head: null,
    length: 0
  };
}

function isEmpty(list) {
  return list.length === 0;
}

function add(list, element) {
  const node = { element, next: null };

  if (isEmpty(list)) {
    list.head = node;
  } else {
    let current = list.head;
    while (current.next !== null) {
      current = current.next;
    }
    current.next = node;
  }

  list.length++;
}

function remove(list, element) {
  let previous = null;
  let current = list.head;

  while (current !== null && current.element !== element) {
    previous = current;
    current = current.next;
  }

  if (current === null) return;

  if (previous !== null) {
    previous.next = current.next;
  } else {
    list.head = current.next;
  }

  list.length--;
}

function contains(list, element) {
  let currEls = list.head
  while (currEls) {
    currEls = currEls.next
    if (currEls.element === element) {
      return true;
    } else {
      return false;
    }
  }
  return false
}

function getAt(list, index) {
  let currEls = list.head;
  let countInd = 0;
  if (index == 0) return currEls.element;
  if (list.length == 0) return undefined;
  if (index > list.length) return undefined;
  while (countInd < index) {
    countInd++;
    //get next element
    currEls = currEls.next
    if (countInd == index) {
      return currEls.element;
    }
  }
  return undefined;
}

Your browser information:

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

Challenge Information:

Implement Linked List Operations - Implement Linked List Operations
https://www.freecodecamp.org/learn/javascript-v9/lab-linked-list-operations/implement-linked-list-operations`Preformatted text`

Try the following example, it ends up with error, instead of outputing undefined.

const list = initList();
console.log(getAt(list, 0))