How to create and initialize a 2d array of characters with different row lengths in Node.js

2 Answers

0 votes
// Initialize a 2D array with different row lengths
const charArray2D = [
    ['N', 'o', 'd', 'e', '.', 'j', 's'],
    ['p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g'],
    ['l', 'a', 'n', 'g', 'u', 'a', 'g', 'e']
];

charArray2D.forEach(row => {
    row.forEach(char => {
        process.stdout.write(char + " ");
    });
    console.log();
});



/*
run:

N o d e . j s 
p r o g r a m m i n g 
l a n g u a g e

*/

 



answered Feb 8, 2025 by avibootz
0 votes
// Initialize a 2D array with different row lengths
const charArray2D = [
    ['N', 'o', 'd', 'e', '.', 'j', 's'],
    ['p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g'],
    ['l', 'a', 'n', 'g', 'u', 'a', 'g', 'e']
];

// Iterate over each row
charArray2D.forEach(row => {
    // Convert the row to a string
    console.log(row.join(''));
});



/*
run:

Node.js
programming
language

*/

 



answered Feb 8, 2025 by avibootz
...