# Need help understanding why array in console.log is not displayed as expected

**URL:** <https://discourse.processing.org/t/need-help-understanding-why-array-in-console-log-is-not-displayed-as-expected/43213>\
**Category:** Beginners\
**Created:** [November 15, 2023, 8:43am UTC](https://discourse.processing.org/t/need-help-understanding-why-array-in-console-log-is-not-displayed-as-expected/43213 "2023-11-15T08:43:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjjochennn](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@jjjochennn](https://discourse.processing.org/u/jjjochennn)\
**Post date:** [November 15, 2023, 8:43am UTC](https://discourse.processing.org/t/need-help-understanding-why-array-in-console-log-is-not-displayed-as-expected/43213/1 "2023-11-15T08:43:58Z")

</div>

Hello!

the console.log in my p5 script is not showing what I expect and I do not understand, why.  
Basically I want to create a random square matrix of zeroes and ones, but in the (rare) case the function returns only zeroes, I want to force at least one value to 1.  
I wanted to confirm the behaviour of my code with console.log and check the array before and after the update.

```auto
function setup() {
  createCanvas(400, 400);
  let a = getRandomMatrix(3)
}

function getRandomMatrix(_size) {
  let _matrix = [];
  let _isValid = false;
  let _randVal;
  //fill the _matrix with random values 0 and 1:
  for (let i = 0; i < _size; i++) {
    for (let j = 0; j < _size; j++) {
      _index = _size * i + j;
      _randVal = int(random(0, 0)); // range is normally (0,2), but for testing I force all values to 0.
      _matrix[_index] = _randVal;
      //if any of the _matrix elements has a value > 0, then _matrix is valid:
      if (_randVal > 0) {
        _isValid = true;
      }
    }
  }
  //if all elements of _matrix are equal to 0
  //set one random element of _matrix to 1:

//HERE IS THE STRANGE BEHAVIOUR - I EXPECT THE CONSOLE.LOG TO BE DIFFERENT 
//BEFORE AND AFTER I FORCE ONE ELEMENT TO 1. BUT THE OUTPUT IS THE SAME
//SHOWING ONE ELEMENT TO BE 1 EVEN BEFORE THE IF-STATEMENT:
  console.log(_isValid, "1: ", _matrix);
  if (!_isValid) {
    _matrix[floor(random(_matrix.length))] = 1;
  }
  console.log(_isValid, "2: ", _matrix);
  return _matrix;
}

```

I hope my explanation makes any sense and you can tell me where my thinking/understanding is flawed.

many thanks,  
Jochen

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 15, 2023, 9:06am UTC](https://discourse.processing.org/t/need-help-understanding-why-array-in-console-log-is-not-displayed-as-expected/43213/2 "2023-11-15T09:06:14Z")

</div>

> [@Strange access to data class](https://discourse.processing.org/t/strange-access-to-data-class/40063/6):
>
> Both outputs in my browser got the expected values: [1, 2, 3] & [3, 1, 2]. But b/c you’re printing an array, if a browser by chance delays the actual printing to the console, it’s possible that you’ll see the array’s most current values for all outputs. As a workaround you can extract the arrays values when printing it using the spread ... syntax: console.log(...myTest.groupe);

---

<div class="post-metadata">

**Author:** ![jjjochennn](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@jjjochennn](https://discourse.processing.org/u/jjjochennn)\
**Post date:** [November 15, 2023, 9:48am UTC](https://discourse.processing.org/t/need-help-understanding-why-array-in-console-log-is-not-displayed-as-expected/43213/3 "2023-11-15T09:48:57Z")

</div>

thanks for the quick reply!  
I take that this is a kind of strange p5 behavior that one just needs to know?  
In the browser’s console the array-elements are indeed shown as expected.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 15, 2023, 9:53am UTC](https://discourse.processing.org/t/need-help-understanding-why-array-in-console-log-is-not-displayed-as-expected/43213/4 "2023-11-15T09:53:50Z")

</div>

> [@jjjochennn](#):
>
> I take that this is a kind of strange p5 behavior…

Rather it’s browser JavaScript itself.
