Implement Linked List Operations - What is "element"?

Tell us what’s happening:

I am confused on what “element” exactly is?
Isn’t it a key:value pair, which is included in every single “next:” element.

I found another user using the method which succeeds in the end (1st in the function). My solution removes the “else” block and doesn’t succeed, even when returning false by default.

Can someone please explain what the “element” actually is and if this is a bug or not? because I am genuinely confused

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 currEl = list.head;

  /* This makes Test #6 SUCCEED, even when it doesn't cycle through every "next:" Element
  while (currEl){
    currEl = currEl.next;
    if (currEl.element === element){
      return true;
    } else{
      return false;
    }
  }
  */

  /* This makes Test #6 FAIL, even though it cycles through every "next:" Element and triggers: "return false" in the end
  while (currEl){
    currEl = currEl.next;
    if (currEl.element === element){
      return true;
    }
  }
  */

  return false;
}

function getAt(list, index) {
  if (index < 0 || list.length === 0){
    return undefined;
  }
  let currList = list.head;
  while (index > 0){
    currList = currList.next;
    if (!currList){
      return undefined;
    }
    index--;
  }
  return currList.element;
}

function insertAt(list, index, element) {
  if (index < 0 || list.length < index) return;

  const node = {element, next: null};
  if (index === 0){
    node.next = list.head;
    list.head = node;
    list.length++;
    return;
  }

  let listEl = list.head;
  let previous;
  while (index > 0){
    previous = listEl;
    listEl = listEl.next;
    index--;
  }
  previous.next = node;
  node.next = listEl;
  node.element = element;
  
  list.length++;
}

function removeAt(list, index) {
  if (index < 0 || list.length < index) return;
}

function clear(list) {

}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:152.0) Gecko/20100101 Firefox/152.0

Challenge Information:

Implement Linked List Operations - Implement Linked List Operations

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-linked-list-operations/69708d59833ceded7d00541f.md at main · freeCodeCamp/freeCodeCamp · GitHub

Imagine that element as the content of the node. The “next” is just the pointer for the next Object with a different content (or “nowhere” (null)).

In a three node code it would look like this:

const linkedList = {
  head: {
    element,
    next: {
      element,
      next: {
        element,
        next: null
      }
    }
  },
  length: 3
};

It´s like the head is the train itself and the wagons are the nodes. The elements are the wagon types (for example), but it can be anything.
(Btw in your case you can only move in one direction from the train, because it´s a single linked list.)

const linkedList = {
  head: {
    wagonType: "civil",
    next: {
       wagonType: "post",
      next: {
         wagonType: "wood logs",
        next: null
      }
    }
  },
  length: 3
};

As you may see, it can be very hard to follow if you have hundred wagons, so that´s why you learning the logic.
That´s why the content of the element inside the nodes doesn´t really matter in this lab.

Objects can store many different things, even functions. It doesn´t have to be a key:value pair all the time.

Ok thank you! that makes it more clearer for me, but then it just confirms the fact, that the solution for test #6 is broken, because it only scans for one “element” and if the first one isn’t the element it’s looking for, it just returns “false” which is another problem that I had with this quiz.

Or do I severely misunderstand what the contains function is supposed to do? Because for me it means: "Scan every list inside your linked list and if it doesn’t find the element you’re looking for, THEN it should return false.