# AppLab Issue with Lists?

**URL:** <https://forum.code.org/t/applab-issue-with-lists/36603>\
**Category:** CS Principles\
**Tags:** applab\
**Created:** [June 28, 2022, 3:52pm UTC](https://forum.code.org/t/applab-issue-with-lists/36603 "2022-06-28T15:52:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![npesola](https://avatars.discourse-cdn.com/v4/letter/n/a9adbd/32.png) [@npesola](https://forum.code.org/u/npesola)\
**Post date:** [June 28, 2022, 3:52pm UTC](https://forum.code.org/t/applab-issue-with-lists/36603/1 "2022-06-28T15:52:59Z")

</div>

Can someone tell me if this scenario is a mistake on my part or a glitch with AppLab?

1. I set a variable called classList, containing a list of names of students.  
_var classList = [“student1”, “student2”, …];_
2. I assigned this variable to originalList.  
_var originalList = classList;_
3. I then manipulate originalList in various ways.

**Am I right to assume that classList should stay the same after this?** Right now I have a program where it changes when I change the originalList, and I don’t know why.

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mitch/32/2678_2.png) [@Mitch](https://forum.code.org/u/Mitch)\
**Post date:** [June 28, 2022, 4:30pm UTC](https://forum.code.org/t/applab-issue-with-lists/36603/2 "2022-06-28T16:30:05Z")

</div>

Hi -  
Your assumption that  
var originalList = classList  
will make a copy of the original array is incorrect.  
What this does is to create a new variable that refers to the same array as the original.  
It sounds like your intention is to make a copy of the originalList array.  
This video shows some ways to do that…  
[https://www.youtube.com/watch?v=EeZBKv34mm4](https://www.youtube.com/watch?v=EeZBKv34mm4)

---

<div class="post-metadata">

**Author:** ![npesola](https://avatars.discourse-cdn.com/v4/letter/n/a9adbd/32.png) [@npesola](https://forum.code.org/u/npesola)\
**Post date:** [June 28, 2022, 4:57pm UTC](https://forum.code.org/t/applab-issue-with-lists/36603/3 "2022-06-28T16:57:51Z")

</div>

Thanks Mitch.

I fixed it with a simple traversal to create the new list.

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [July 5, 2022, 2:10am UTC](https://forum.code.org/t/applab-issue-with-lists/36603/4 "2022-07-05T02:10:12Z")

</div>

> [@Everything You Wanted to Know About App Lab Arrays, But Were Afraid to Ask](https://forum.code.org/t/everything-you-wanted-to-know-about-app-lab-arrays-but-were-afraid-to-ask/34277):
>
> Everything You Wanted to Know About Arrays, But Were Afraid to Ask. [] have two uses. One is to create an array and potentially initialize it. So for example: var a = [1,2,3]; That creates an array and assigns it to a variable. Is that variable an array now? No, it is not. Trick question. Variables don’t have types in Javascript. a is a variable that has an array value. It could just as easily hold a number or a string. Variables don’t care. I see a lot of var cats = [getColumn("Cats","Nam…

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [July 5, 2022, 5:02pm UTC](https://forum.code.org/t/applab-issue-with-lists/36603/5 "2022-07-05T17:02:41Z")

</div>

One thing that has not been asked is why do you need a second copy?

Instead of making a copy so you can change it, create a new list as you make changes.

---

<div class="post-metadata">

**Author:** ![npesola](https://avatars.discourse-cdn.com/v4/letter/n/a9adbd/32.png) [@npesola](https://forum.code.org/u/npesola)\
**Post date:** [July 8, 2022, 4:55pm UTC](https://forum.code.org/t/applab-issue-with-lists/36603/6 "2022-07-08T16:55:35Z")

</div>

I want that list to be accessible and unchanged for other functions as the app is used.

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [July 8, 2022, 5:33pm UTC](https://forum.code.org/t/applab-issue-with-lists/36603/7 "2022-07-08T17:33:55Z")

</div>

I would recommend taking a page out of the Functional Programming Paradigm. Any function that modifies a list will take a list as a parameter and return a newly created list with the changes leaving the parameter alone.

When you call the function you can decide what to do with the new list. What this does is set up lists to be immutable (never change unless you explicitly assign.) In general, that will lower the number of errors encountered in sharing lists between functions.

---

<div class="post-metadata">

**Author:** ![IMPixel](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/impixel/32/6744_2.png) [@IMPixel](https://forum.code.org/u/IMPixel)\
**Post date:** [July 11, 2022, 2:09pm UTC](https://forum.code.org/t/applab-issue-with-lists/36603/8 "2022-07-11T14:09:58Z")

</div>

classList.slice() to make a copy of it:

```auto
var originalList = classList.slice()

```

`var originalList = classList` does not create a copy of it, but puts classList into originalList, and when you will modify classList, originalList will change. The same thing happens with originalList.

---

<div class="post-metadata">

**Author:** ![npesola](https://avatars.discourse-cdn.com/v4/letter/n/a9adbd/32.png) [@npesola](https://forum.code.org/u/npesola)\
**Post date:** [July 20, 2022, 3:10am UTC](https://forum.code.org/t/applab-issue-with-lists/36603/9 "2022-07-20T03:10:07Z")

</div>

I want to thank everyone who has responded. My grasp of this is tenuous, but I did grab incites from everyone’s feedback if not a whole understanding of how to apply it to my problems and project.

It definitely encourages me to post more to this forum when I know I can define my problem well enough.

---

<div class="post-metadata">

**Author:** ![letti42](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@letti42](https://forum.code.org/u/letti42)\
**Post date:** [July 22, 2022, 8:47pm UTC](https://forum.code.org/t/applab-issue-with-lists/36603/10 "2022-07-22T20:47:57Z")

</div>

I’ll be honest I did not think this occurred normally until it happened to me in my own project!

I had to set the new array to `Array.from(oldArray)`

That is weird though how it happens.
