Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to create fixed size empty matrix JavaScript

3 Answers

0 votes
function Print(arr2d, rows, cols) {
    for (let i = 0; i < rows; i++) {
        let s = "";
        for (let j = 0; j < cols; j++) {
            s +=  arr2d[i][j] + " ";
        }
        console.log(s);
    }
    console.log();
}
 
const rows = 4;
let matrix = new Array(rows);
 
const cols = 3;
for (let i = 0; i < rows; i++) {
    matrix[i] = new Array(cols);
}
 
Print(matrix, rows, cols);
 
 
  
/*
run:
    
undefined undefined undefined 
undefined undefined undefined 
undefined undefined undefined 
undefined undefined undefined 
      
*/

 



answered Apr 29, 2024 by avibootz
edited May 16, 2024 by avibootz
0 votes
function Print(arr2d, rows, cols) {
    for (let i = 0; i < rows; i++) {
        let s = "";
        for (let j = 0; j < cols; j++) {
            s +=  arr2d[i][j] + " ";
        }
        console.log(s);
    }
    console.log();
}
 
const rows = 4;
const cols = 3;
 
let matrix = [];
 
for (let i = 0; i < rows; i++) {
    matrix[i] = new Array(cols);
}
 
Print(matrix, rows, cols);
 
 
  
/*
run:
    
undefined undefined undefined 
undefined undefined undefined 
undefined undefined undefined 
undefined undefined undefined 
      
*/

 



answered Apr 29, 2024 by avibootz
edited May 16, 2024 by avibootz
0 votes
function Print(arr2d, rows, cols) {
    for (let i = 0; i < rows; i++) {
        let s = "";
        for (let j = 0; j < cols; j++) {
            s +=  arr2d[i][j] + " ";
        }
        console.log(s);
    }
    console.log();
}
 
const rows = 4;
const cols = 5;
 
const matrix = Array(rows).fill(0).map(() => Array(cols).fill(0));
 
Print(matrix, rows, cols);
 
 
  
/*
run:
    
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
      
*/

 



answered May 16, 2024 by avibootz

Related questions

2 answers 152 views
2 answers 118 views
2 answers 110 views
1 answer 78 views
1 answer 128 views
1 answer 148 views
1 answer 146 views
...