How to convert a character (flatten) matrix to a single string in JavaScript

1 Answer

0 votes
function flattenMatrix(matrix) {
  return matrix.flat().join('');
}

function main() {
  const matrix = [
    ['H', 'e', 'l', 'l', 'o'],
    [' ', 'W', 'o', 'r', 'l', 'd'],
    ['!']
  ];

  const output = flattenMatrix(matrix);
  console.log("Flattened string:", output);
}

main();



/*
run:

Flattened string: Hello World!

*/

 



answered Jan 10 by avibootz

Related questions

...