 # Can I accept matrix array from input without build in func?

Hello, so this is my technical test and it looks like I need input and even help from you guys

Test:
Create a function that accepts a multidimensional array in the form of a matrix. The matrix is ​​then transposed to become as follows:

input: [
[1,2,3],
[1,2,3],
[1,2,3]
]

output: [
[1,1,1],
[2,2,2],
[3,3,3]
]

Rules:
Must manage input
Dont use build in function

Iam just trying like this,
But unfortunately that might be against the rules because no input occurs. Possible

``````function arrMulti(arr) {
let output = [];
arr.forEach(function(part, i, theArr) {
theArr[i] = [i+1, i+1, i+1];
output = theArr;
})

console.log(output);
}

arrMulti([
[1,2,3],
[1,2,3],
[1,2,3],
]);
``````

Is there a better way?

This may work for the exact 3x3 matrix used in your example, but what about a 2x2 or 4x4 matrix? What if the numbers aren’t all the same or increment by more than one? Unless the test said that the function will only ever need to handle this one specific type of 3x3 matrix you will need to make your function a little more robust.

1 Like

what means ‘Dont use build in function’. Does it mean not to use array methods?
From what i can gather, the challenge ask you to flip the matrix, so its columns become the rows. Basically, each first element in a row, which is practically the first column, will become the first row of the resulting mitrix, second elements will become the second row and so forth; i.e. all elements with index “0” will be part of the first array in the output, elements with index “1” will be part of the second array.
I was able to produce a solution(as far as i understood the terms), using 2 nested loops. On every iteration of the outer loop id create a row(an array) and within the inner loop, id push all elements from a single column. Then id push the resulting row(which represents a column of the input matrix) in the output matrix.

1 Like
``````let arrMulti = [ [1,2,3], [1,2,3], [1,2,3], ];
var output = [];

for(let j = 0; j < 3; j ++){
let data = []
for(let i = 0; i < 3; i ++){
console.log("arrMulti[%d][%d] = %d", i,j,arrMulti[i][j])
data[i] = arrMulti[i][j]
}
output[j] = data
}
console.log(output)

``````

you can try this demo

1 Like

the solution is correct, but you wanna use the matrix size as a loop counter, instead of hardcore it as “3”, so it can work for any matrix size