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,990 questions

51,935 answers

573 users

How to print matrix rows in a chain pairs (first + second, second + third) with JavaScript

1 Answer

0 votes
function print_row(matrix, row) {
    const cols = matrix[0].length
    
    let s = "";
    for (let j = 0; j < cols; j++) {
        s += matrix[row][j] + " ";
    }
    console.log(s);
}

  
   
const matrix = [ [ 4,  7,  9, 18, 29], 
                 [ 7,  9, 18, 29,  4], 
                 [ 9, 18, 29,  4,  7], 
                 [18, 29,  4,  7,  9], 
                 [29,  4,  7,  9, 18] ];
  
const rows = matrix.length;


for (let i = 0; i < rows - 1; i++) {
    print_row(matrix, i);
    print_row(matrix, i + 1);
    console.log("-----------");  
}


      
        
        
/*
run:
        
4 7 9 18 29 
7 9 18 29 4 
-----------
7 9 18 29 4 
9 18 29 4 7 
-----------
9 18 29 4 7 
18 29 4 7 9 
-----------
18 29 4 7 9 
29 4 7 9 18 
-----------
        
*/

 



answered Jun 25, 2023 by avibootz
...