-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayToMatrix.js
More file actions
36 lines (33 loc) · 803 Bytes
/
Copy patharrayToMatrix.js
File metadata and controls
36 lines (33 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function arrToMatrix(arr){
if (!Array.isArray(arr) ){
console.log("arr's prototype is not Array.")
} else {
var topRow = arr[0]
if (!Array.isArray(topRow)){
console.log("arr[0]'s prototype is not Array.")
}
else {
console.log("arr[0]'s prototype is Array.")
var matColNum = maxLength(arr)
console.log("maxLength: " + matColNum)
var arr2 = arr.map(function(arri,i,mat){return fillArray(arri,matColNum)})
return arr2
}
}
}
function maxLength(arr){
return arr.reduce(
(acc,curr)=>(
(acc <= curr.length) ? curr.length : acc
),0
)
}
function fillArray(arr,len){
//console.log("Before:", arr)
while (len - arr.length > 0){
arr.push("")
}
//console.log("After:", arr)
return arr
}
module.exports = arrToMatrix