javascript 2D array

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

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();
}

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? :slight_smile:

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.

// 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;
    }

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();
}

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.

1 Like