# javascript 2D array

**URL:** <https://forum.kirupa.com/t/javascript-2d-array/643062>\
**Category:** programming\
**Created:** [June 2, 2020, 3:56pm UTC](https://forum.kirupa.com/t/javascript-2d-array/643062 "2020-06-02T15:56:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Umair\_Ashraf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/umair_ashraf/32/11705_2.png) [@Umair\_Ashraf](https://forum.kirupa.com/u/Umair_Ashraf)\
**Post date:** [June 2, 2020, 3:56pm UTC](https://forum.kirupa.com/t/javascript-2d-array/643062/1 "2020-06-02T15:56:49Z")

</div>

hi guys,

i am working through one of the hackerrank challenge, but kind of stuck while pushing the value into INTEGER\_ARRAY, i am getting error as TypeError: Cannot read property ‘push’ of undefined.

as per my understanding we are using arrow function therefore we can use the parent function variables…(INTEGER\_ARRAY) as well so i am not sure why this error is coming up  
any suggestion please  
link: [https://www.hackerrank.com/challenges/dynamic-array/problem](https://www.hackerrank.com/challenges/dynamic-array/problem)

code: below

```
'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}

/*
 * Complete the 'dynamicArray' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts following parameters:
 * 1. INTEGER n
 * 2. 2D_INTEGER_ARRAY queries
 */

*function dynamicArray(n, queries) {*
* // Write your code here*
* *
* let INTEGER_ARRAY=[];*
* let lastans=0;*
* // make a string w from integer*
* let w = new Array(n) +''*
* let queriesx=new Array(queries) +''*
* // split on the bases of next line *
* queriesx= queriesx.split('\n')*
* const k = w.split(' ')[0]*

* for (let i=0; i<k; i++){*

*INTEGER_ARRAY[i]=[]*

* }*

*queriesx.forEach(e1=>*
*{*
* const [d,x,y] = e1.split('').map(Number)*
* //below is formula to extract the sequence number in array integer_array*
*const seq =((x ^ lastans)% k );*

***switch(d){***
 ***// if d is 1***
case 1:
***INTEGER_ARRAY[seq].push(y);***

 case 2:
***INTEGER_ARRAY[seq].push(y);***
 ***let value =y%INTEGER_ARRAY.length***
 ***lastans = value***
 **}**

}

)

return INTEGER_ARRAY;
}

function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' ');

    const n = parseInt(firstMultipleInput[0], 10);

    const q = parseInt(firstMultipleInput[1], 10);

    let queries = Array(q);

    for (let i = 0; i < q; i++) {
        queries[i] = readLine().replace(/\s+$/g, '').split(' ').map(queriesTemp => parseInt(queriesTemp, 10));
    }

    const result = dynamicArray(n, queries);

    ws.write(queries);

    //ws.write(result.join('\n') + '\n');

    ws.end();
}
```

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 2, 2020, 4:44pm UTC](https://forum.kirupa.com/t/javascript-2d-array/643062/2 "2020-06-02T16:44:15Z")

</div>

This is an interesting problem! Your code currently is formatted in a strange way with a lot of \* characters that makes it difficult to see what is a comment and what may be a part of your actual output.

Do you have a cleaned-up version you can share? 🙂

---

<div class="post-metadata">

**Author:** ![Umair\_Ashraf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/umair_ashraf/32/11705_2.png) [@Umair\_Ashraf](https://forum.kirupa.com/u/Umair_Ashraf)\
**Post date:** [June 2, 2020, 5:02pm UTC](https://forum.kirupa.com/t/javascript-2d-array/643062/3 "2020-06-02T17:02:31Z")

</div>

Hi Kirupa,  
The question at hackerrank(for this problem) is kind of confusing and took me alot time just to understand what is being asked, still i am not sure if i got it correctly.  
so basically,

- we received “n” and “queries” as integers in dynamic array function,
- we using n to create an array
- from queries we have to check the condition and perform a task , i split queries per each line with variable [d,x,y], d is used to check condition (1 or 2) and x is the position in the 2D array can be x[0] or x[1] and y is the actual value that we need to push in either x[0] or x[1] and that makes up the array INTEGER\_ARRAY which will be returned in the end

the below is the actual code that i wrote a part from the default that is part of the question at [hackerrank.com](http://hackerrank.com).

_// My code here_

> ```
> function dynamicArray(n, queries) {      
> let INTEGER_ARRAY=[];
> let lastans=0;
> let w = new Array(n) +''
> let queriesx=new Array(queries) +''
> queriesx= queriesx.split('\n')
> const k = w.split(' ')[0]
> 
> for (let i=0; i<k; i++){
> INTEGER_ARRAY[i]=[]
> }
> queriesx.forEach(e1=>
> {
> const [d,x,y] = e1.split('').map(Number)
> const seq =((x ^ lastans)% k );
> 
> switch(d){
> case 1:
> INTEGER_ARRAY[seq].push(y);
> case 2:
> INTEGER_ARRAY[seq].push(y);
> let value =y%INTEGER_ARRAY.length
> lastans = value
> }
> }
> )
> return INTEGER_ARRAY;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Umair\_Ashraf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/umair_ashraf/32/11705_2.png) [@Umair\_Ashraf](https://forum.kirupa.com/u/Umair_Ashraf)\
**Post date:** [June 2, 2020, 5:17pm UTC](https://forum.kirupa.com/t/javascript-2d-array/643062/4 "2020-06-02T17:17:58Z")

</div>

and below is the default code that comes with the question and without any of my code

> ```
> 'use strict';
> 
> const fs = require('fs');
> 
> process.stdin.resume();
> process.stdin.setEncoding('utf-8');
> 
> let inputString = '';
> let currentLine = 0;
> 
> process.stdin.on('data', function(inputStdin) {
> inputString += inputStdin;
> });
> 
> process.stdin.on('end', function() {
> inputString = inputString.split('\n');
> 
> main();
> });
> 
> function readLine() {
> return inputString[currentLine++];
> }
> 
> /*
> * Complete the 'dynamicArray' function below.
> *
> * The function is expected to return an INTEGER_ARRAY.
> * The function accepts following parameters:
> * 1. INTEGER n
> * 2. 2D_INTEGER_ARRAY queries
> */
> 
> function dynamicArray(n, queries) {
> // Write your code here
> 
>   
> }
> 
> function main() {
> const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
> 
> const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' ');
> 
> const n = parseInt(firstMultipleInput[0], 10);
> 
> const q = parseInt(firstMultipleInput[1], 10);
> 
> let queries = Array(q);
> 
> for (let i = 0; i < q; i++) {
> queries[i] = readLine().replace(/\s+$/g, '').split(' ').map(queriesTemp => parseInt(queriesTemp, 10));
> }
> 
> const result = dynamicArray(n, queries);
> 
> ws.write(result.join('\n') + '\n');
> console.log(n)
> ws.end();
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 3, 2020, 5:21am UTC](https://forum.kirupa.com/t/javascript-2d-array/643062/5 "2020-06-03T05:21:39Z")

</div>

I spent some time looking at this, and I am able to see the error you are running into. Not being able to test locally in my browser makes this difficult for me to figure out what is wrong, though. Here is where I am stuck:

The statement `INTEGER_ARRAY[seq].push(y);` throws the **undefined** error. `INTEGER_ARRAY[0].push(y);` throws the same **undefined** error as well. This leads me to think that the value of `k` that you use to populate `INTEGER_ARRAY` with empty arrays is being set to something wrong like 0. Now, what is going on here to do this? I can’t figure out since I don’t have a Node environment setup locally.
