For example:
const matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
Should become:
const result = [
[1,4,7],
[2,5,8],
[3,6,9]
]
here is y solution for this challenge
function r(mat) {
return mat.reduce((acc, row, idx) => {
return [...acc, mat.map(itm => itm[idx])]
}, [])
}
let res = r(matrix)
console.log({res})
Or it can be written as one row:
const r = mat => mat.reduce((acc, row, idx) => [...acc, mat.map(itm => itm[idx])], [])
let res = r(matrix)
console.log({res})
No comments:
Post a Comment