matrixDiagonal.js with lots of dry run( *** Please Please Please REVISE ***)

 /*Matrix Diagonal

Write a function which takes a 2 dimentional array of size nxn where n > 0 and changes its diagonal according to the following conditions

if an element e < 0 replace it with 0
If element e >= 0 replace it with 1
Input
The first line contains n, denoting the number of lists.

This is followed by n lines. Each line contains n integers separated by a space

Output
n lines, each line representing a list of numbers separated by a space.

Example
Input:

4
2 0 1 4
0 -1 1 10
0 0 0 0
1 2 3 4

Output:

1 0 1 4
0 0 1 10
0 0 1 0
1 2 3 1

*/

let fs = require("fs");

let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');

function readLine() {
idx++;
return data[idx - 1].trim();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////

// This part is only for taking inputs

const rows = parseInt(readLine()); // 4 is the no. of rows

let rowArray = []; // we will push the arrays in this matrix

for (let i = 0; i < rows; i++) {

subArray = readLine().split(" ");

// //console.log(subArray); [ '2', '0', '1', '4' ]
// [ '0', '-1', '1', '10' ]
// [ '0', '0', '0', '0' ]
// [ '1', '2', '3', '4' ] all green color element because they
have not been converted to integer.

for (let j = 0; j < rows; j++) {

subArray[j] = parseInt(subArray[j]); //

}

// //console.log(subArray); [ 2, 0, 1, 4 ]
// [ 0, -1, 1, 10 ]
// [ 0, 0, 0, 0 ]
// [ 1, 2, 3, 4 ] all yellow color element now

rowArray.push(subArray);

}

//console.log(rowArray); //[ [ 2, 0, 1, 4 ], [ 0, -1, 1, 10 ], [ 0, 0, 0, 0 ], [ 1, 2, 3, 4 ] ]

// This is the actual operation

for (let i = 0; i < rows; i++) // 0) i=0< 4 true, go inside the forloop.
// 1) i=1<4 true, go inside the forloop.
// 2) i=2<4 true, go inside the forloop.
// 3) i=3<4 true, go inside the forloop.
// 4) i=4<4 false, exit the entire forloop.


{

if (rowArray[i][i] >= 0) // 0) rowArray[0][0] = 2 and 2>=0 , so go inside the if condition

// 1) rowArray[1][1] = -1 and -1>=0 false, so go to else condition

// 2) rowArray[2][2] = 0 and 0>=0 , so go inside the if condition

// 3) rowArray[3][3] = 4 and 4>=0, so go inside the if condition


rowArray[i][i] = 1; // 0) rowArray[0][0] =1 , from 2 it is changed to 1 now iterate the
forloop for i++, i.e,, i is 1 now

// 2) rowArray[2][2] = 1, from 0 it is changed to 1 now iterate the
forloop for i++, i.e., i is 3 now

// 3) rowArray[3][3] = 1 , from 4 it is changed to 1 now iterate the
forloop for i++, i.e., i is 4 now.

else

rowArray[i][i] = 0; // 1) rowArray[1][1] = 0 , from -1 it is changed to 0 now iterate
the forloop for i++, i.e., i is 2 now

}


//console.log(rowArray) [ [ 1, 0, 1, 4 ], [ 0, 0, 1, 10 ], [ 0, 0, 1, 0 ], [ 1, 2, 3, 1 ] ]


for (let i = 0; i < rowArray.length; i++)
{

console.log(...rowArray[i]) // this ... is called spread operator
// 0) for i=0 (...rowArray[0]) = [1,0,1,4] will be converted
to 1 0 1 4 and same with other
iterations.

}

//////////////////////////////////////////////////////////////////////////////////////////////
TERMINAL:













Comments