# Problem with arrays

**URL:** <https://forum.makecode.com/t/problem-with-arrays/23832>\
**Category:** Help\
**Created:** [November 1, 2023, 7:44pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832 "2023-11-01T19:44:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/luke/32/32519_2.png) [@Luke](https://forum.makecode.com/u/Luke)\
**Post date:** [November 1, 2023, 7:44pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/1 "2023-11-01T19:44:58Z")

</div>

How do I add an array to another array but also randomise it? Another way to put it would be how to shuffle an array.

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [November 2, 2023, 4:07pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/2 "2023-11-02T16:07:56Z")

</div>

The simplest (and, arguably, fastest) way to shuffle an array is to walk through the array and, for each element, swap it with another.

> **[ShuffleArray](https://arcade.makecode.com/16710-13932-47924-07004)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

 ![arcade-screenshot (9)](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/2/2da4e0afd43936e8ae3781fe395ee5c8889cc7dc.png)

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [November 2, 2023, 4:08pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/3 "2023-11-02T16:08:36Z")

</div>

Here’s a quick implementation I did, it might not be the most optimal one but in that case I’m sure @Blobbey or @Vegz78 will submit their better versions.

 ![arcade-screenshot](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/8/819f4e794f823c2cdf288794ce7829073c102c4b.png)

First I just concatenated the arrays with the `add` method, and after that I put the elements of the array into a new array in a random order until all of the elements were depleted. You can take a look at the code yourself (and/or reuse it) here:

> **[Shuffle arrays](https://arcade.makecode.com/S84894-49091-73241-37123)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [November 2, 2023, 4:12pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/4 "2023-11-02T16:12:01Z")

</div>

Since I am too old for crosswords and sudoku, but find challenges like this interesting, here’s my bid:

> **[add'n'shuffle](https://arcade.makecode.com/41062-68886-28781-52341)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

Probably not very efficient, waiting for the elders to optimize further… 😉

PS: Added sort() just because this is the only such array operation I have found already there in MCA. Sort() modifies the original array, my functions copy. To make ShuffleList() modify the original array instead, remove the tempList entirely and replace it were used with the inputList parameter.  
PS2: How to get the native sort() to sort numbers above 9 correctly(9, 10, 11,…)?  
PS3: Some more related information:  
[https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/14](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/14)  
[https://forum.makecode.com/t/sorting-algorithm-extension-for-blocks/16260](https://forum.makecode.com/t/sorting-algorithm-extension-for-blocks/16260)

---

<div class="post-metadata">

**Author:** ![randomuser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/randomuser/32/32505_2.png) [@randomuser](https://forum.makecode.com/u/randomuser)\
**Post date:** [November 2, 2023, 4:15pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/5 "2023-11-02T16:15:11Z")

</div>

you could create a new array, pick a random value of the original array to add and then remove, and repeat until empty!

---

<div class="post-metadata">

**Author:** ![salieri](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/salieri/32/7275_2.png) [@salieri](https://forum.makecode.com/u/salieri)\
**Post date:** [November 2, 2023, 4:16pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/6 "2023-11-02T16:16:51Z")

</div>

> [@Luke](#):
>
> How do I add an array to another array but also randomise it? Another way to put it would be how to shuffle an array.

[Here is a solution](https://arcade.makecode.com/S49234-49519-18995-29329)

Click on the Show Data Simulator to see the array randomized when you press A.

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [November 2, 2023, 5:40pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/7 "2023-11-02T17:40:00Z")

</div>

Rewarding to experience the shared interest on this forum for the great fun of algorithms!!!

Somebody should create a Wordle for algorithms, “The Daily Algo”, or something! 😉

---

<div class="post-metadata">

**Author:** ![Luke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/luke/32/32519_2.png) [@Luke](https://forum.makecode.com/u/Luke)\
**Post date:** [November 2, 2023, 5:40pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/8 "2023-11-02T17:40:02Z")

</div>

Thank you all for replying!

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [November 2, 2023, 5:40pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/9 "2023-11-02T17:40:48Z")

</div>

Quick reimplementation of that `add` function, I realized that it could be more efficient. Optimized it from O(2n) to O(n) time complexity by removing a loop because it’s unnecessary

 ![arcade-screenshot (1)](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/2/2b2137693dbb30dbb12c50d7a539ada884daa7e8.png)

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [November 2, 2023, 10:21pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/10 "2023-11-02T22:21:34Z")

</div>

Great, @Sarge! I attempted the same, actually on the basis of a previous similar post from you, but I did not manage to do this without modifying the original list1 itself by reference all the way back to before the function call…

Would this be possible with a copy/call by value as well?

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [November 3, 2023, 4:59pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/11 "2023-11-03T16:59:55Z")

</div>

I’m not sure honestly. Could you elaborate a bit further on what you’re trying to do?

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [November 6, 2023, 7:16pm UTC](https://forum.makecode.com/t/problem-with-arrays/23832/12 "2023-11-06T19:16:19Z")

</div>

Hi @Sarge,

I was trying to add two arrays together without using 2 loops and still keep the original array intact(copy).

I found out from one of your earlier posts that this was possible with concat():

> [@Anyone know how to join two arrays together?](https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/15):
>
> I know it’s kind of late, but JavaScript has a convenient Array.prototype.concat() method. It returns a value without modifying the original array. Given array1 = [1, 2] & array2 = [3, 4]: let array3 = array1.concat(array2); ‘array3’ will be defined as [1, 2, 3, 4] (this method is stackable, so you can do array1.concat(array2).concat(array3)... In blocks you need to write your own method, as one is not in place. You could implement it something like this (pseudocode) : function add\_arrays…

Still, I have not found out how to do this with only blocks/without grey blocks…

Here, illustrated with the functions AddList\_loop(), AddList\_no\_lopp() and AddList\_reference(), which are 2 loops, concat and same as yours above, respectively:

> **[ListAdd](https://arcade.makecode.com/55041-60792-68848-28261)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

As you can see, in the last one - AddList\_reference() and the same as yours above -, _result_ seems only to be a reference to _array1_ via the parameter _list1_, and the original _array1_ is overwritten.
