2018-10-10 18:03:03 -04:00
---
title: Identity matrix
id: 5a23c84252665b21eecc7eb1
challengeType: 5
2019-08-28 16:26:13 +03:00
forumTopicId: 302290
2018-10-10 18:03:03 -04:00
localeTitle: Единичная матрица
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
< i > Единичная матрица</ i > является квадратной матрицей размера \ (n \ times n \), где диагональные элементы - все < b > 1</ b > s (одни), а все остальные элементы - все < b > 0</ b > s (нули). \ begin {bmatrix} 1 & 0 & 0 \ cr 0 & 1 & 0 \ cr 0 & 0 & 1 \ cr \ end {bmatrix} Напишите функцию, которая принимает число «n» в качестве параметра и возвращает единичную матрицу порядок nx n.
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
< section id = 'instructions' >
Write a function that takes a number < code > n</ code > as a parameter and returns the identity matrix of order \( n \times n \).
2018-10-10 18:03:03 -04:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: < code > idMatrix</ code > should be a function.
testString: assert(typeof idMatrix=='function');
- text: < code > idMatrix(1)</ code > should return an array.
testString: assert(Array.isArray(idMatrix(1)));
- text: < code > idMatrix(1)</ code > should return < code > [ [ 1 ] ]</ code > .
testString: assert.deepEqual(idMatrix(1),results[0]);
- text: < code > idMatrix(2)</ code > should return < code > [ [ 1, 0 ], [ 0, 1 ] ]</ code > .
testString: assert.deepEqual(idMatrix(2),results[1]);
- text: < code > idMatrix(3)</ code > should return < code > [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]</ code > .
testString: assert.deepEqual(idMatrix(3),results[2]);
- text: < code > idMatrix(4)</ code > should return < code > [ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ]</ code > .
testString: assert.deepEqual(idMatrix(4),results[3]);
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-08-28 16:26:13 +03:00
function idMatrix(n) {
2018-10-10 18:03:03 -04:00
// Good luck!
}
```
< / div >
2019-08-28 16:26:13 +03:00
### After Tests
2018-10-10 18:03:03 -04:00
< div id = 'js-teardown' >
```js
2019-08-28 16:26:13 +03:00
let results=[[ [ 1 ] ],
[ [ 1, 0 ], [ 0, 1 ] ],
[ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ],
[ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ]]
2018-10-10 18:03:03 -04:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-08-28 16:26:13 +03:00
function idMatrix(n) {
return Array.apply(null, new Array(n)).map(function (x, i, xs) {
return xs.map(function (_, k) {
return i === k ? 1 : 0;
})
});
}
2018-10-10 18:03:03 -04:00
```
2019-08-28 16:26:13 +03:00
2018-10-10 18:03:03 -04:00
< / section >