---
id: 69a04712866a37ea8f38b6bd
title: Step 21
challengeType: 1
dashedName: step-21
---

# --description--

Now, you should test out the `remove` function.

Remove any of the numbers from the list by calling the `remove` function with `myList` and the number to remove, then log `myList` to the console using `console.log(JSON.stringify(myList, null, 2))` to see the complete updated list.

With that, your linked list workshop is complete!

# --hints--

You should remove any of the existing numbers using the `remove` function.

```js
assert.match(__helpers.removeJSComments(code),/remove\s*\(\s*myList\s*,\s*(42|43|44)\s*\)/);
```

You should log `myList` to the console again, this time using `JSON.stringify(myList, null, 2)`.

```js
assert.match(__helpers.removeJSComments(code), /console\.log\s*\(\s*JSON\.stringify\s*\(\s*myList\s*,\s*null\s*,\s*2\s*\)\s*\)/);
```

# --seed--

## --seed-contents--

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

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

function add(list, element) {
    const node = {
        element: 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--;
    
}

const myList = initList();
console.log(isEmpty(myList));
add(myList, 42);
add(myList, 43);
add(myList, 44);
console.log(myList);
console.log(isEmpty(myList));
--fcc-editable-region--

--fcc-editable-region--
```

# --solutions--

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

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

function add(list, element) {
    const node = {
        element: 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--;
    
}

const myList = initList();
console.log(isEmpty(myList));
add(myList, 42);
add(myList, 43);
add(myList, 44);
console.log(myList);
console.log(isEmpty(myList));
remove(myList, 43);
console.log(JSON.stringify(myList, null, 2));
```
