working link
https://stackblitz.com/edit/typescript-omoaq4
code =>
https://stackblitz.com/edit/typescript-omoaq4
code =>
//draw a matrix and exchange two column values in the matrix based on given row index
class matrix
{
static rm=[];
static rc=[];
static drawMatrix(r:number,c:number)
{
//n2
var k=0;
for(let i=0,k=0;i<r;i++)
{
this.rc=[];
for(let j=0;j<c;j++,k++)
{
this.rc.push(String.fromCharCode(64+(k+1)));
}
this.rm.push(this.rc);
}
}
static exchangeRowData(rowIndex,col1,col2)
{
let currentRow=this.rm[rowIndex];
let temp=currentRow[this.getIndex(currentRow,col1)];
let index=this.getIndex(currentRow,col2);
currentRow[this.getIndex(currentRow,col1)]=currentRow[index];
currentRow[index]=temp;
}
static getIndex(arry,value)
{
for(var i=0;i<arry.length;i++)
{
if(arry[i]===value)
{
return i;
}
}
}
static getMatrix()
{
return this.rm;
}
}
matrix.drawMatrix(4,4);
console.log(matrix.getMatrix());
matrix.exchangeRowData(1,"E","F");
console.log(matrix.getMatrix());
Comments
Post a Comment