Skip to content
47 changes: 47 additions & 0 deletions Exercises.ru.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Упражнения

## Итерирование циклами

Реализуйте функцию `sum(...args)`, которая суммирует все свои аргументы, пятью
разными способами. Примеры вызовов с результатами:
```js
const a = sum(1, 2, 3) // a === 6
const b = sum(0) // b === 0
const c = sum() // c === 0
const d = sum(1, -1, 1) // d === 1
const e = sum(10, -1, -1, -1) // e === 7
```

1. Цикл `for`
2. Цикл `for..of`
3. Цикл `while`
4. Цикл `do..while`
5. Метод `Array.prototype.reduce()`

## Итерирование по двумерному массиву

6. Найдите максимальный элемент в двумерном массиве
```js
const m = max([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
console.log(m); // 9
```

## Итерирование объектов-справочников

7. При помощи цикла `for..in` перебрать объект-справочник с датами рождения и
смерти людей и вернуть справочник с продолжительностью их жизни. Например:
```js
const persons = {
lenin: { born: 1870, died: 1924 },
mao: { born: 1893, died: 1976 },
gandhi: { born: 1869, died: 1948 },
hirohito: { born: 1901, died: 1989 },
};
console.log(ages(persons));
// {
// lenin: 54,
// mao: 83,
// gandhi: 79,
// hirohito: 88,
// }
```
8 changes: 5 additions & 3 deletions Exercises/1-for.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const sum = (...args) => {
// Use for loop and accumulator variable
// to calculate sum of all given arguments
// For example sum(1, 2, 3) should return 6
let res = 0;
for (let i = 0; i < args.length; i++) {
res += args[i];
}
return res;
};

module.exports = { sum };
8 changes: 5 additions & 3 deletions Exercises/2-for-of.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const sum = (...args) => {
// Use for..of loop and accumulator variable
// to calculate sum of all given arguments
// For example sum(1, 2, 3) should return 6
let res = 0;
for (const value of args) {
res += value;
}
return res;
};

module.exports = { sum };
8 changes: 5 additions & 3 deletions Exercises/3-while.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const sum = (...args) => {
// Use while loop and accumulator variable
// to calculate sum of all given arguments
// For example sum(1, 2, 3) should return 6
let result = 0;
while (args.length > 0) {
result += args.pop();
}
return result;
};

module.exports = { sum };
9 changes: 6 additions & 3 deletions Exercises/4-do-while.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';

const sum = (...args) => {
// Use do..while loop and accumulator variable
// to calculate sum of all given arguments
// For example sum(1, 2, 3) should return 6
if (args.length === 0) return 0;
let result = 0;
do {
result += args.pop();
} while (args.length > 0);
return result;
};

module.exports = { sum };
5 changes: 1 addition & 4 deletions Exercises/5-reduce.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';

const sum = (...args) => 0;
// Use Array.prototype.reduce method
// to calculate sum of all given arguments
// For example sum(1, 2, 3) should return 6
const sum = (...args) => args.reduce(((acc, cur) => acc + cur), 0);

module.exports = { sum };
32 changes: 32 additions & 0 deletions Exercises/6-matrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const max = matrix => {
let value = matrix[0][0];
for (const index in matrix) {
const line = matrix[index];
for (const index2 in line) {
const colum = line[index2];
if (colum > value) value = colum;
}
}
return value;
};

/*const max = matrix => {
let value = matrix[0][0];
for (const arr of matrix) {
for (const i of arr) {
if (i > value) value = i;
}
}
return value;
};*/

/*const max = matrix => {
const modeMatrix = matrix
.map(arr => arr.reduce((a, b) => (a > b ? a : b)))
.reduce((a, b) => (a > b ? a : b));
return modeMatrix;
};*/

module.exports = { max };
13 changes: 13 additions & 0 deletions Exercises/6-matrix.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
({
name: 'max',
length: [220, 300],
cases: [
[[[10]], 10],
[[[1, 2], [3, 4], [5, 6]], 6],
[[[-1, 1], [2, -1], [-1, 0]], 2],
],
test: max => {
const src = max.toString();
if (!src.includes('for (')) throw new Error('Use for loop');
}
})
12 changes: 12 additions & 0 deletions Exercises/7-ages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const ages = persons => {
const age = {};
for (const key in persons) {
const person = persons[key];
age[key] = person.died - person.born;
}
return age;
};

module.exports = { ages };
24 changes: 24 additions & 0 deletions Exercises/7-ages.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
({
name: 'ages',
length: [150, 190],
cases: [
[
{
lenin: { born: 1870, died: 1924 },
mao: { born: 1893, died: 1976 },
gandhi: { born: 1869, died: 1948 },
hirohito: { born: 1901, died: 1989 },
}, {
lenin: 54,
mao: 83,
gandhi: 79,
hirohito: 88,
}
]
],
test: ages => {
const src = ages.toString();
if (!src.includes('for (')) throw new Error('Use for..in loop');
if (!src.includes(' in ')) throw new Error('Use for..in loop');
}
})
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Different implementation of iterations as a code abstraction

[![Массивы, объекты, классы, прототипы](https://img.youtube.com/vi/VBMGnAPfmsY/0.jpg)](https://www.youtube.com/watch?v=/VBMGnAPfmsY)
[![Массивы, объекты, классы, прототипы](https://img.youtube.com/vi/VBMGnAPfmsY/0.jpg)](https://www.youtube.com/watch?v=VBMGnAPfmsY)
[![Итерирование, циклы и итераторы](https://img.youtube.com/vi/lq3b5_UGJas/0.jpg)](https://www.youtube.com/watch?v=lq3b5_UGJas)
2 changes: 1 addition & 1 deletion Solutions/3-while.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const sum = (...args) => {
let value = 0;
while (args.length > 0) {
value += args.shift();
value += args.pop();
}
return value;
};
Expand Down
2 changes: 1 addition & 1 deletion Solutions/4-do-while.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const sum = (...args) => {
if (args.length === 0) return 0;
let value = 0;
do {
value += args.shift();
value += args.pop();
} while (args.length > 0);
return value;
};
Expand Down
15 changes: 15 additions & 0 deletions Solutions/6-matrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const max = matrix => {
let value = matrix[0][0];
for (let i = 0; i < matrix.length; i++) {
const row = matrix[i];
for (let j = 0; j < row.length; j++) {
const cell = row[j];
if (value < cell) value = cell;
}
}
return value;
};

module.exports = { max };
12 changes: 12 additions & 0 deletions Solutions/7-ages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const ages = persons => {
const data = {};
for (const name in persons) {
const person = persons[name];
data[name] = person.died - person.born;
}
return data;
};

module.exports = { ages };