/
Identity_matrix.js
41 lines (32 loc) · 1009 Bytes
/
Identity_matrix.js
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
37
38
39
40
41
#!/usr/bin/env node
/*
Given an integer input `n`, output the `n x n` identity matrix. The identity matrix is one that has `1`s spanning from the top left down to the bottom right. You will write a program or a function that will return or output the identity matrix you constructed. Your output may be a 2D array, or numbers separated by spaces/tabs and newlines.
**Example input and output**
1: [[1]]
2: [[1, 0], [0, 1]]
3: [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
4: [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
5: [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]
1
===
1
2
===
1 0
0 1
3
===
1 0 0
0 1 0
0 0 1
*/
var x = function(number) {
return [...Array(number)].map(function(value, key1, arr) {
return arr.map(function(value, key2) {
return +(key1 == key2);
});
});
}
var y=n=>[...Array(n)].map((V,K,A)=>A.map((v,k)=>+(K==k)))
x(4);
y(4);